Note: I agree with esteemed colleague, and mentor, SAKruykov, that MDI based WinForm applications are not good practice now.
A few comments:
1. I think your question confuses
moving vs.
re-sizing of the Child Forms: you can't "move" a Form by its right or bottom; you can only resize.
a.
... under 'normal' conditions, when the Child Form starts off completely contained in the MDI Parent Form ... you can't resize an MDI Child Form outside its MDI Parent boundaries.
b. so: the real issue here must be: moving the Child Form.
c. the "dirty fact" here, a flaw, imho, in MDI architecture, is the fact that:
once you have moved part of an MDI Child Form outside its MDI Parent Form, you can re-size it !
2.
You also may have just "crashed against" another one of the real flaws in the MDI implementation: even if you set the 'AutoScroll property of the MDI Parent Form to 'false: moving an MDI Child Form so it extends, outside the Parent Forms' boundaries: will cause scrollbars to appear on the MDI Parent Form.
So: what to do if you must use MDI, and you want to keep the Child Forms in the MID Parent Form window boundaries when you Move them ?
There are several events of the child Form you could possibly use
ClientSizeChanged
LocationChanged
SizeChanged
RegionChanged
None of these Events expose a custom EventArgument structure that would allow you to cancel the action they reflect. None of them expose a symmetric pair of Begin/End events, with a "cancel" option in the 'Begin... Event as some other .NET control's events do.
So you are going to have, somehow, undo the effect of a Move when the Child Form goes outside the bounds of the MDI Parent Form, and that's complicated by the "dirty fact" mentioned above.
You can exploit this technique for determining whether the MDI Child Form is, or is not, fully contained in the MDI Parent Form: whenever the MDI Child Form is completely within the MDI Parent Form: then the intersection of the bounding rectangles (when derived correctly from the appropriate co-ordinate space of the two Forms) of those two Forms will be equal to the bounding rectangle of the Child Form (details to follow).
So in the MDI Parent Form ... here named 'Form1 ... :
Form2 f2 = new Form2();
public static Rectangle MdiParentBounds { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
f2.MdiParent = this;
f2.Show();
MdiParentBounds = this.ClientRectangle;
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
MdiParentBounds = this.ClientRectangle;
}Now in the MDI Child Form: ... here named Form2 ...:private Rectangle childBounds;
private Rectangle interSectRect;
private void Form2_LocationChanged(object sender, EventArgs e)
{
childBounds = this.Bounds;
interSectRect = Rectangle.Intersect(Form1.MdiParentBounds, childBounds);
if ( interSectRect == childBounds)
{
MessageBox.Show("child form is contained");
}
else
{
MessageBox.Show("child form is not contained");
}
}
This should give you enough information, and code, to get started on being able to detect when an MDI Child Form extends outside the boundaries of its MDI Parent Form.
What you are going to have to do to move the Child Form which has gone outside the boundaries back so that it is within the boundaries, is left for you to go to work on.
Hint: you are going to determine which "edges" of the Child Form are outside, and what offset you need to use, vertically and horizontally, to relocate it back. And if it's been re-sized also, while outside the MDI Parent's bounds: welcome to further complications !
Of course: you could take the "cheap way out," and just change the size of the MDI Child Form to be some size you know will fit in the MDI Parent Form, and snap it back to upper-left in the MDI Parent: but, surely, you are an ambitious programmer who won't just settle for the "easy way out" :)
best, Bill