Click here to Skip to main content
13,054,615 members (61,175 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 31 Jul 2008

Have fun with the MDI client area

, 31 Jul 2008
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 is currently a student at Central Washington University, studying for a B.S. in Actuarial Science with plans to graduate in June, 2019. He has been writing code for almost 30 years in more than a dozen programming languages. Previously, he worked for a mid-size brokerage firm as the company's programming department doing VB6, VB.Net, ASP, HTML, XML and SQL. He also held the Series 7 and Series 66 brokerage licenses.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
allothernamesaretaken2-Dec-08 19:11
memberallothernamesaretaken2-Dec-08 19:11 
AnswerSetting background color... Pin
ionmnoi4-Aug-08 3:33
memberionmnoi4-Aug-08 3:33 
AnswerSetting background colour... Pin
GraGra_3331-Jul-08 21:23
memberGraGra_3331-Jul-08 21:23 
GeneralRe: Setting background color gradient for MDI clientarea Pin
Ilíon4-Aug-08 3:38
memberIlíon4-Aug-08 3:38 
GeneralOk, THAT is spiffy Pin
TechBearSeattle4-Aug-08 5:35
memberTechBearSeattle4-Aug-08 5:35 
GeneralRe: Ok, THAT is spiffy Pin
Ilíon4-Aug-08 7:47
memberIlíon4-Aug-08 7:47 
GeneralRe: Gradient for MDI clientarea Pin
Damian J. Suess16-Feb-09 17:09
memberDamian J. Suess16-Feb-09 17:09 
QuestionRe: Gradient for MDI clientarea Pin
Ilíon16-Feb-09 19:02
memberIlíon16-Feb-09 19:02 
GeneralRe: Gradient for MDI clientarea Pin
Ilíon18-Feb-09 6:10
memberIlíon18-Feb-09 6:10 
GeneralMDI client can also be put into other containers, it seems... [modified] Pin
supercat931-Jul-08 14:36
membersupercat931-Jul-08 14:36 
GeneralAlso see... Pin
Ravi Bhavnani31-Jul-08 9:40
memberRavi Bhavnani31-Jul-08 9:40 

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

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

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