Custom Controls Design Time Support Part 13: Adding Snaplines

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.


1- Add a designer.

2- override Snaplines property and add your own custom ones.


public override System.Collections.IList SnapLines
        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;


3- Don't forget to add the Designer attribute to the Control definition


public partial class TestUserControl : UserControl


The final looks as this.

Final look

So good luck and happy coding with custom controls design time.

UserControlDesigner.cs (7.70 kb)







4 responses to “Custom Controls Design Time Support Part 13: Adding Snaplines”

  1. Aren Avatar

    Hi like this articel but can you handout to me how to do it in VB.NET?

  2. Leo Avatar

    Very Cool.
    But… having the constituent controls declared as private (automatically, by the UserControl Designer), how this can be achieved?

    .Change the declarations to internal (I don’t like it, but it is better than public!)
    .Create a internal property which tells the offset? (better, but still not very elegant).
    .Other way?


  3. Aren Avatar

    Found it out already

  4. Mike Avatar

    note that the baseline of the login label doesn’t actually match up with the baseline of the text in the username label since it’s snapping to the bottom of the username label, not the baseline of the text… is the baseline of the text in the textbox or label exposed somehow?

Leave a Reply

Your email address will not be published. Required fields are marked *