I would consider using the Control.Dock
property along with one of the DockStyle
enumeration values.
You might need to play with the Layout
too, so that you may layout your form s controls differently depending on the DockStyle
selected.
You will need, in my point of view, to consider the Control.Location
property so that you get to know which DockStyle
value to dock your form with.
EDIT #1
Your Windows Form has a Dock
property as it inherits from Control
.
Let s consider the following :
Each time your form comes closer to your right-side of the screen, for example, or of the MDI container, you want to dock right, right ? (Little word play here... =P) So, you have to subscribe to the Control.LocationChanged
event.
private void myForm_LocationChanged(object sender, EventArgs e) {
if (this.Location.X > 900) then
this.Dock = DockStyle.Right;
else if (this.Location.X < 150) then
this.Dock = DockStyle.Left;
else if (this.Location.Y > 600) then
this.Dock = DockStyle.Bottom;
else if (this.Location.Y < 150) then
this.Dock = DockStyle.Top;
else
this.Dock = DockStyle.None;
}
Indeed, instead of constant values, you should use the current desktop resolution and calculate a ratio from it where you want your docking to occur.
***Disclaimer:****This code is provided as-is and has not been tested. This algorithm is hopefully enough to guide you through the docking process as you need it. Further assistance may be brought upon request.* =)
It seems the Form.DesktopLocation
property is the righter tool for the job as for your main window, meaning your MDI container, for instance. As for the other windows, I would go along with something that looks like the code sample provided.
Does this help?
EDIT #2
If you want to prevent Form s overlapping, perhaps the Control.BringToFront()
method could do it before or after your call to the Control.Show()
method, depending on what works best for you.