Change spacing of snaplines in Visual Studio
Asked Answered
C

5

11

Is it possible to change the amount of space snaplines put between controls in a WinForms project in Visual Studio?

For example, when I slide a textbox up to another textbox (one above the other), there's 6 pixels of space between the controls. I'd like there to be 5 pixels of space between them when they snap to each other.

Thanks.


EDIT:

Thanks for all of the answers. I thought it might be helpful to summarize the approaches:

Option 1

Set the Layout Mode to SnapLines (default) under Tools > Options > Windows Forms Designer > General and follow Igby Largeman's guidance in the accepted answer.

Option 2

Set the Layout Mode to SnapToGrid and choose the spacing via Default Grid Cell Size. Thanks to Joe Caffeine and Marc Stober for this answer.

Supplemental approach 1

Hold the ALT key when dragging controls to avoid using snapping altogether as Vigness.N suggested.

Supplemental approach 2

Use arrow keys to move controls in pixel increments.

Supplemental approach 3

Johannes Frank also suggests adding placeholder controls to assist with initial snaplines.

Convent answered 4/9, 2009 at 0:12 Comment(0)
C
14

Note that snaplines are used both for spacing (blue lines) and for alignment (pink lines).

Yes, you can control the amount of space the snaplines suggest, but you do this by altering the Margin and Padding properties of the controls.

If you slide one control toward another, the length of the snapline is the sum of the Margin of the two controls.

If you slide a control toward the border of its containing control, the length of the snapline is the sum of the Margin of the control and the Padding of the containing control.

This is discussed here.

Edit: There is a special case when the Form is the parent container and the Form's padding is 0,0,0,0.

Note
If the form's Padding property is set to 0,0,0,0, the Windows Forms Designer gives the form a shadowed Padding value of 9,9,9,9. To override this behavior, assign a value other than 0,0,0,0.

Conditioner answered 26/3, 2013 at 23:59 Comment(0)
C
7

There are no snaplines that help you set the spacing between controls, they assist in aligning control edges and text. Instead, the designer pays attention to the Margin property of the control. The default for a Button is (3, 3, 3, 3), it will snap in place creating a 3 pixel gap from the adjacent control.

Best to try it yourself. Drop a label and a button, change the button's Margin to (3, 10, 3, 3) and move the Button below label up and down. You'll see it now snaps in place creating a 10 pixel gap.

Compressibility answered 22/3, 2013 at 21:46 Comment(4)
This in incorrect in two ways. One, there are snaplines for spacing - they are the blue lines the designer draws when the control you're moving or resizing comes close to another control. Two, the suggested spacing is determined by the summing the margins/paddings of both controls in question.Conditioner
Hmm, no, I don't see blue snap lines that mark the gap. Yes on summing the margins, depends on the margins settings on the adjacent control of course. You downvoted this answer for that?Compressibility
Yes I did Hans - two factually incorrect statements justify a downvote, surely? The real question is why did three people upvote a misleading answer? (There are definitely snaplines for spacing - read the documentation I linked to. Whether they are blue on your installation may depend on your windows colour settings)Conditioner
Thanks, Hans. Your answer was helpful.Convent
C
1

There is a setting to control the grid spacing under tools -> options -> Windows Forms Designer.

Cissie answered 4/9, 2009 at 0:25 Comment(2)
Not sure about 2008, but at least in 2010, it looks like that's just for LayoutMode=SnapToGrip; the question is about the spacing for LayoutMode=SnapLines.Sibell
This was very useful. Between the accepted answer and yours, I think I understand how to control spacing. Thanks!Convent
S
1

You can press and hold the Alt key when dragging controls and release the key after the control is placed in the desired point, so that it does not snap anywhere by default.

Spigot answered 27/3, 2013 at 11:10 Comment(1)
It's good to remember this option. It could come in handy. Thanks.Convent
Y
0

Obviously there is no option setting. You can however achieve your goal with these two meassures: First: Move the control after the drop with the arrow keys. Second: You could place helper buttons at the left right top or bottom side on your container. Snapline will use these as a reference and if they are spaced 4 pixel your new controls will also be 4 pixel appart. You could also press control with an arrow key to move to the next suggested snapline position. Spacing of down to 1 pixel is possible with this. Finally delete your helper buttons.

Hope it helps.

Yiyid answered 20/3, 2013 at 19:34 Comment(1)
I could see how this strategy might help in some situations. Thanks.Convent

© 2022 - 2024 — McMap. All rights reserved.