Another new feature added in the Design Time Environment in visual studio 2005 and 2008 for windows forms and windows user controls designers is the Snaplines.
What is a snapline?
A snapline is a dynamically generated UI layout guide line. Snaplines are generated during control move operations in the designer. The location of other controls on the design surface provide the lines.
All controls by default inherit the default the Snaplines which appear while moving that interact with Tops, Lefts, Bottoms and Rights of other controls, with the form border, spacings from other controls and some other. One remarkable snapline is the one that appears while moving a Label control beside TextBox controls the purple snapline that aligns the text position instead of the control position. [more]
Now using thr sample control I used in the previous part , I added two labels and two texboxes to simulate a Login control, like the following:
By default when placing this control on a windows form and start moving it around there are no snaplines to interact with the inner labels and textboxes in this sample I will show how to implement custom Snaplines for this usercontrol.
Steps:
1- Add a designer.
2- override Snaplines property and add your own custom ones.
[code:c#]
public override System.Collections.IList SnapLines
{
get
{
IList snaplines = base.SnapLines; //get old snaplines and then add new ones
snaplines.Add(new SnapLine(SnapLineType.Baseline, _control.lblUserName.Bottom));
snaplines.Add(new SnapLine(SnapLineType.Baseline, _control.lblPassword.Bottom));
return snaplines;
}
}
[/code]
3- Don't forget to add the Designer attribute to the Control definition
[code:c#]
[Designer(typeof(UserControlDesigner))]
public partial class TestUserControl : UserControl
[/code]
The final looks as this.
So good luck and happy coding with custom controls design time.
Leave a Reply