MDI .NET Forms Include Docked Controls And Smaller MDIClient

By Robbe Morris

This little code snippet demonstrates how to include various docked controls on your MDIParent form and then dynamically force the MDIClient to respect the available docking space when set to DockStyle.Fill at runtime.

The docking order in windows forms .net and the management thereof has always been a sore spot for me.  The technique below is one way to get around the limitations of what can exist on a parent MDI form while still rendering the MDIClient properly.

What I did here was set the MDI Parent to NOT be an MDI container in the Visual Studio .NET 2005 Designer.  I put all my various controls and docked them as needed.  Then, in my form's constructor, I set the form to be an MDIParent at runtime.  All that was then needed was to find the MDIClient (dynamically created for us by .NET) and set it to fill the remaining docking space.  This appears to force the MDIClient to respect
the existing docking orders of the previously created controls.

public Form2()
 {
   InitializeComponent();
   this.IsMdiContainer = true;

   MdiClient client = null;

   foreach (Control control in this.Controls)
   {
     if (control.GetType() == typeof(MdiClient))
     {
       client = (MdiClient)control;
       client.Dock = DockStyle.Fill;
       return;
     }
   }
 }
Popularity  (2626 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of EggHeadCafe.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP
Create New Account
Article Discussion: MDI .NET Forms Include Docked Controls And Smaller MDIClient
Robbe Morris posted at Tuesday, June 05, 2007 3:00 PM
reply