I have a canvas (Panel Control) in my WinForms app where users can drag things like textbox's, labels etc around. But I want to make it easier for them to more precisely align the objects. I've read into it and Adorners seem to be the way to go? But, apparently it's only for WPF. WPF is not an option for me, unfortunately.
What I'm trying to accomplish is to have lines pop up every time the user drags an object around in the canvas... Just how they work in the Windows Forms Designer View.
I'd appreciate any help at all.
Thank you.
Thank you all for your answers.
I've managed to come up with my own solution. The code is down below. There are no "lines" yet, but I'll get around to it oneday...
Label l = (Label)sender;
foreach (Control control in Canvas.Controls)
{
if (l.Location.X > control.Location.X + control.Size.Width && l.Location.X < control.Location.X + control.Size.Width + 5)
l.Location = new Point(control.Location.X + control.Size.Width + 5, l.Location.Y);
else if (l.Location.X < control.Location.X - l.Size.Width && l.Location.X > control.Location.X - l.Size.Width - 5)
l.Location = new Point(control.Location.X - l.Size.Width - 5, l.Location.Y);
else if (l.Location.Y > control.Location.Y + control.Size.Height && l.Location.Y < control.Location.Y + control.Size.Height + 5)
l.Location = new Point(l.Location.X, control.Location.Y + control.Size.Height + 5);
else if (l.Location.Y < control.Location.Y - control.Size.Height && l.Location.Y > control.Location.Y - control.Size.Height - 5)
l.Location = new Point(l.Location.X, l.Location.Y - 5);
this.Update();
}
The above code must be placed inside the Control_MouseMove Event, and ofcourse, you still need your own code that actually moves the controls.
The code above will snap the control you're dragging 5 pixels to the right, left, top or bottom of the nearest control.