Daniel Ng
Email Me Contact Me

You're welcome to email me at dng11@sympatico.ca.

While I do like hearing from you, I am not interested in SEO services nor offshoring my business, so please don't offer me any.

Follow me on Twitter Subscribe to my RSS feed

How To Always Maximize MDI Child Form
Originally posted by Daniel Ng on December 31, 2012.

Let's suppose you have to create an MDI application in .NET where all the MDI child forms are always maximized and cannot be resized. Naturally, the child form's Minimize, Maximize and Close buttons will have to be hidden. Sounds easy, right? Well, I can assure you that the solution is in fact easy. The process of deriving that solution, however, may prove otherwise. Google this and you will find plenty of complaints and people coming up with all sorts of "creative" solutions. To spare you the pain, here is what all you really need to do:

Sample MDI Application

To test this, I have created a simple MDI application as shown below. There are 3 ToolStrip buttons on the parent form that will open respective child form on the button click event.

Child Form 1 Child Form 2 Child Form 3

On Parent MDI Form

Here's the code behind:

Private Sub ButtonOpenForm1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpenForm1.Click
With FormChild1
.MdiParent = Me
For Each frm As Form In Me.MdiChildren
If frm.Name <> .Name Then frm.WindowState = FormWindowState.Minimized
Next
.WindowState = FormWindowState.Maximized
.Show()
End With
End Sub

Private Sub ButtonOpenForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpenForm2.Click
With FormChild2
.MdiParent = Me
For Each frm As Form In Me.MdiChildren
If frm.Name <> .Name Then frm.WindowState = FormWindowState.Minimized
Next
.WindowState = FormWindowState.Maximized
.Show()
End With
End Sub

Private Sub ButtonOpenForm3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs Handles ButtonOpenForm3.Click
With FormChild3
.MdiParent = Me
For Each frm As Form In Me.MdiChildren
If frm.Name <> .Name Then frm.WindowState = FormWindowState.Minimized
Next
.WindowState = FormWindowState.Maximized
.Show()
End With
End Sub

The key here is to minimize other child forms first before you show your targeted child form. Otherwise, the system will display the child form's Minimize, Maximize and Close buttons.

On Each Child Form

On each child form's Form Load event, you must programmatically set both the WindowState and ControlBox properties. Setting them at design-time WILL NOT work. No other form properties (e.g., FormBorderStyle, MaximizeBox, MinimizeBox, etc.) need to be set. They are superfluous in this case.

Example,
Private Sub FormChild_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
Me.ControlBox = False
End Sub

Voilà! You're done.

Copyright © 2013 www.xcentricway.ca. All rights reserved.