I have a C# WinForms MDI application that has a few MDI child windows. Users are able to move or resize the individual windows. The issue is that when they move the windows to the bounds of the MDI parent, scrollbars appear on the MDIParent window and the user can drag the child windows outside the bounds of the MDI parent. Is there any way to change this behavior so that the child windows are always inside the parent window and no scrollbars are ever created? I know there are ways to "pop" the child windows back inside the parent window开发者_开发知识库 by overriding the OnMove event. I want the windows to stay inside even when the user is moving the window. Is there any way to do this?
On your child forms, handle the FormResize event, with something like this:
private void Form1_Resize(object sender, EventArgs e)
{
Size pSize = this.ParentForm.ClientSize;
Size maxAllowed = new Size(pSize.Width - this.Left, pSize.Height - this.Top);
// Resize the child if it goes out of bounds
if (this.Height > maxAllowed.Height)
this.Height = maxAllowed.Height;
if (this.Width > maxAllowed.Width)
this.Width = maxAllowed.Width;
}
probably not great results depending on wht you want.
I would probably redo it so that it moves te form back in bounds instead of resizing it.
精彩评论