Click here to Skip to main content
Click here to Skip to main content

Have fun with the MDI client area

, 31 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
How to modify your client area properties, center controls, and do other fun tricks.


When you designate a form as an MDI container, the .NET framework docks an MdiClient control on your form. Unfortunately, the framework does not expose any of the properties of this control, which makes it difficult to change things like the client area background color or to center controls. Difficult at first glance, that is.

How it is done

The MdiClient control is a control just like any other, even though it is managed implicitly by the framework. As a control, it can be found in the Form's Controls collection. This is how you can get it using VB:

Public Class Form1

    Private ClientControl As MdiClient

    Public Sub New()

        ClientControl = Nothing
        For Each Ctl As Control In Me.Controls
            ClientControl = TryCast(Ctl, MdiClient)
            If ClientControl IsNot Nothing Then Exit For
    End Sub

End Class

Here is the C# version:

public partial class Form1 : Form
    private MdiClient ClientControl;

    public Form1()
        ClientControl = null;
        foreach (Control Ctl in this.Controls)
            ClientControl = Ctl as MdiClient;
            if (ClientControl != null) break;

Once you have verified that ClientControl is not Nothing (or null), you can treat it just like any other control. Perhaps, your application can point to a production database and one used just for testing? Change the BackColor property to show this. Do you want a logo to remain centered when the MDI form is resized? Drop a PictureBox into the client area and then use this code in the Form's Resize event:

PictureBox1.Left = (ClientControl.Width \ 2) - (PictureBox1.Width \ 2)
PictureBox1.Top = (ClientControl.Height \ 2) - (PictureBox1.Height \ 2)


PictureBox1.Left = (int)((ClientControl.Width / 2) - (PictureBox1.Width / 2));
PictureBox1.Top= (int)((ClientControl.Height/ 2) - (PictureBox1.Height/ 2));

Avenues of exploration

When you set IsMdiContainer to true, the framework does more than just add a MdiControl onto the form. You can see this clearly if you try something like this:

If ClientControl IsNot Nothing Then
    ClientControl.Dock = DockStyle.Right
    ClientControl.Width = Me.ClientRectangle.Width \ 2
End If


if (ClientControl != null)
    ClientControl.Dock = DockStyle.Right;
    ClientControl.Width = (int) (this.ClientRectangle.Width / 2)

The part of the form that is exposed is set to SystemColors.AppWorkspace, regardless of what color you give to the BackColor of ClientControl and the form. I have not yet figured out how to change this to match the form's background, but when I do, the ability to dynamically resize the MdiClient would open up some interesting possibilities.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gregory Gadow
Software Developer (Senior)
United States United States
Gregory Gadow lives in Seattle, Washington and has been writing code for almost 25 years in more than a dozen programming languages. He works for a mid-size brokerage firm and holds the Series 7 and Series 66 brokerage licenses, but much prefers working as the company's programming department doing VB6, VB.Net, ASP, HTML, XML and SQL.

Comments and Discussions

GeneralMy vote of 1 Pinmemberallothernamesaretaken2-Dec-08 20:11 
AnswerSetting background color... Pinmemberionmnoi4-Aug-08 4:33 
AnswerSetting background colour... PinmemberGraGra_3331-Jul-08 22:23 
GeneralRe: Setting background color gradient for MDI clientarea PinmemberIlíon4-Aug-08 4:38 
GeneralOk, THAT is spiffy PinmemberTechBearSeattle4-Aug-08 6:35 
GeneralRe: Ok, THAT is spiffy PinmemberIlíon4-Aug-08 8:47 
GeneralRe: Gradient for MDI clientarea PinmemberDamian J. Suess16-Feb-09 18:09 
QuestionRe: Gradient for MDI clientarea PinmemberIlíon16-Feb-09 20:02 
GeneralRe: Gradient for MDI clientarea PinmemberIlíon18-Feb-09 7:10 
GeneralMDI client can also be put into other containers, it seems... [modified] Pinmembersupercat931-Jul-08 15:36 
GeneralAlso see... PinmemberRavi Bhavnani31-Jul-08 10:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 31 Jul 2008
Article Copyright 2008 by Gregory Gadow
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid