Disable MDIchildren moving outside MDIparent client area

Stoffel

Member
Joined
Dec 11, 2004
Messages
16
Location
Belgium
Programming Experience
1-3
Hi,

Is there a way that you cannot move the MDIchildren outside the MDIparent's client area (visible area)?
 
Sure there's a way. It's not as simple as setting a property however.

If you create a form template for all your child forms and include the following code in that template (ie have all you childForms inherit from that form), you can't move the child forms outside of the parentForm's clientRectangle.
VB.NET:
Private oldLoc As Point

Private Sub _Move(ByVal sender As Object, ByVal e As System.EventArgs) _
  Handles MyBase.Move
    If Not Me.ParentForm Is Nothing Then
        If Me.Left < Me.ParentForm.ClientRectangle.Left Or _
           Me.Right >= Me.ParentForm.ClientRectangle.Width - 5 Or _
           Me.Top < Me.ParentForm.ClientRectangle.Top Or _
           Me.Bottom >= Me.ParentForm.ClientRectangle.Height - 5 Then
            Me.Location = oldLoc
        Else
            oldLoc = Me.Location
        End If
    End If
End Sub

Private Sub _Load(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles MyBase.Load
    oldLoc = Me.Location
End Sub
The 5 in the Right and Bottom expressions is the ever-Famous fudgeFactor, used so the scrollbars won't appear in the parentForm.

Of course there are more elegant solutions, this is just a sample.
 
Last edited:
Back
Top