views:

716

answers:

4

The default MDI parent control has a large "desktop" area that can display multiple child forms. Users can drag forms to the edge of this desktop area so that most of the child form is off the screen. (A scroll bar then appears in the MDI parent) I don't like this feature. Is there a way to lock down the edge of the desktop area so that the child forms remain fully visible?

+2  A: 

To clarify, what you say is the "desktop" area of the MDI client is the client area.

You could handle the resize/move event handlers of the child forms and then resize/restrict the movement of the child when it exceeds the bounds of the MDI client area.

casperOne
+2  A: 
  1. Disable the MDI window scrollbars
  2. Hook the OnMove event of all child windows. If the window is moved outside the boundary, "pop" it back along the x and y until it is back inside the parent.
Dave Swersky
See my code below that contains code that does this.
Jeff
+4  A: 

The code I used to implement the answer I selected above:

 Public alreadyMoved As Boolean = False
 Public Const HEIGHT_OF_MENU_STATUS_BARS As Integer = 50
 Public Const WIDTH_OF_MENU_STATUS_BARS As Integer = 141
 Private Sub Form_Move(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Move
        If Not alreadyMoved Then
            alreadyMoved = True

            'If I'm over the right boundry, drop back to right against that edge
            If Me.Location.X + Me.Width > MdiParent.ClientRectangle.Width - WIDTH_OF_MENU_STATUS_BARS Then
                MyBase.Location = New System.Drawing.Point((MdiParent.ClientRectangle.Width - Me.Width - WIDTH_OF_MENU_STATUS_BARS), MyBase.Location.Y)
            End If

            'If I'm over the bottom boundry, drop back to right against that edge
            If Me.Location.Y + Me.Height > MdiParent.ClientRectangle.Height - HEIGHT_OF_MENU_STATUS_BARS Then
                MyBase.Location = New System.Drawing.Point(MyBase.Location.X, (MdiParent.ClientRectangle.Height - Me.Height - HEIGHT_OF_MENU_STATUS_BARS))
            End If

            'If I'm over the top boundry, drop back to the edge
            If Me.Location.Y < 0 Then
                MyBase.Location = New System.Drawing.Point(MyBase.Location.X, 0)
            End If

            'If I'm over the left boundry, drop back to the edge
            If Me.Location.X < 0 Then
                MyBase.Location = New System.Drawing.Point(0, MyBase.Location.Y)
            End If
        End If
        alreadyMoved = False
    End Sub
Jeff
A: 

Could you provide the C# code to do this?

Thanks...

There's nothing fancy about the code so it should auto-convert using any of the online conversion utilities. Try: http://www.developerfusion.com/tools/convert/vb-to-csharp/
Jeff