I would consider using the Control.Dock property along with one of the DockStyle .
You may need to play with Layout so that you can layout control forms in different ways depending on the DockStyle selected.
You, in my opinion, will need to consider the Control.Location property so that you know what DockStyle value to attach your form with.
EDIT NO. 1
Your Windows form has a Dock property because it inherits from Control .
Consider the following:
Every time your form approaches your right side of the screen, for example, or the MDI container, you want to dock to the right, right? (A small word plays here ... = P) So, you should 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; }
In fact, instead of constant values, you should use the current desktop resolution and calculate the coefficient from it where you want your docking to occur.
*** Disclaimer: **** This code is provided as is and has not been tested. This algorithm, we hope, is enough to guide you through the docking process when you need it. Additional assistance is available upon request. * =)
It seems that the Form.DesktopLocation property is a more convenient tool for setting, as for your main window, which means your MDI container, for example, As for other windows, I would agree with something that looks like the provided code sample.
Does it help?
EDIT No. 2
If you want to prevent the form from matching, perhaps the Control.BringToFront() method could do this before or after your call to Control.Show() , depending on what suits you best.
Will marcouiller
source share