views:

152

answers:

2

Hi All

I have an app where users can drag controls around on a Form. But they re asking me for Snap-To lines to make the alignment of controls easier. I have no idea about the snep-to lines and how to implement them - I have looked at:

http://msdn.microsoft.com/en-us/library/ms752100.aspx Adorner's, but it says it's only for WPF. And I tried it in WinForms but (as expected) didn't work.

How can I get snap-to lines (something like the ones in VS) in my app?

Thank you

Bael

+1  A: 

Have you seen this article on CodeProject:

Form Designer

It features snap-to to the grid on the design surface.

Jay Riggs
Although this one snaps to the grid, it's not quite what I'm looking for but I will definately keep this in mind. Thank you Jay :)
baeltazor
+2  A: 

In your move control you could adjust the Left and Top by dividing and then multiplying by the width of your lines:

left = (left/10)*10;
top = (top/10)*10;

It's not perfect but it it simple. Of course since controls don't have a MoveEnd event you'll have to track the MouseButton state or something similiar.

Edit: A better implementation would properly round the division results so 134 = 130 and 136 = 140.

Cory Charlton
Um... That looks to simple to be true :P Thanks for that Cory, I'm gonna see what I can do :)
baeltazor
Often times the best solutions are the simplest :-). I didn't implement this code so I'm very anxious to hear how the idea has (or has not) worked out for you.
Cory Charlton