Reputation: 1
I have a parent mdi window that has two child windows that you can open and close. The parent starts as maximized and I want to keep the children from crossing the boundaries of the parent so the children are 100% on screen at all time.
The parent(mdi) is maximized and I want the children(window forms) window to stay on the screen and not be able to be dragged outside the monitor. Basically how to stop the children from being able to be dragged off screen.
Any thoughts?
Upvotes: 0
Views: 2087
Reputation: 862
This might be an answer to your problem:
protected override void OnMove(EventArgs e)
{
//
// Get the MDI Client window reference
//
MdiClient mdiClient = null;
foreach(Control ctl in MdiParent.Controls)
{
mdiClient = ctl as MdiClient;
if(mdiClient != null)
break;
}
//
// Don't allow moving form outside of MDI client bounds
//
if(Left < mdiClient.ClientRectangle.Left)
Left = mdiClient.ClientRectangle.Left;
if(Top < mdiClient.ClientRectangle.Top)
Top = mdiClient.ClientRectangle.Top;
if(Top + Height > mdiClient.ClientRectangle.Height)
Top = mdiClient.ClientRectangle.Height - Height;
if(Left + Width > mdiClient.ClientRectangle.Width)
Left = mdiClient.ClientRectangle.Width - Width;
base.OnMove(e);
}
Add it in Your MDI child form. It flickers little bit even with double buffering turned on but it is the best solution i have found.
Upvotes: 2