Click here to Skip to main content
15,881,881 members
Articles / Programming Languages / Visual Basic
Article

Have fun with the MDI client area

Rate me:
Please Sign up or sign in to vote.
3.95/5 (12 votes)
31 Jul 2008CPOL1 min read 65.6K   16   11
How to modify your client area properties, center controls, and do other fun tricks.

Introduction

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:

VB
Public Class Form1

    Private ClientControl As MdiClient

    Public Sub New()
        InitializeComponent()

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

End Class

Here is the C# version:

C#
public partial class Form1 : Form
{
    private MdiClient ClientControl;

    public Form1()
    {
        InitializeComponent();
        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:

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

C#:

C#
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:

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

C#:

C#
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.

License

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


Written By
United States United States
Gregory Gadow recently graduated from Central Washington University with a B.S. that combined economics and statistical analysis, and currently works for the Washington Department of Fish & Wildlife as an IT developer. He has been writing code for 30 years in more than a dozen programming languages, including Visual Basic, VB.Net, C++, C#, ASP, HTML, XML, SQL, and R.

Comments and Discussions

 
GeneralMy vote of 1 Pin
allothernamesaretaken2-Dec-08 19:11
allothernamesaretaken2-Dec-08 19:11 
AnswerSetting background color... Pin
ionmnoi4-Aug-08 3:33
ionmnoi4-Aug-08 3:33 
AnswerSetting background colour... Pin
Graeme_Grant31-Jul-08 21:23
mvaGraeme_Grant31-Jul-08 21:23 
GeneralRe: Setting background color gradient for MDI clientarea Pin
Ilíon4-Aug-08 3:38
Ilíon4-Aug-08 3:38 
GeneralOk, THAT is spiffy Pin
Gregory Gadow4-Aug-08 5:35
Gregory Gadow4-Aug-08 5:35 
GeneralRe: Ok, THAT is spiffy Pin
Ilíon4-Aug-08 7:47
Ilíon4-Aug-08 7:47 
GeneralRe: Gradient for MDI clientarea Pin
Damian Suess16-Feb-09 17:09
Damian Suess16-Feb-09 17:09 
QuestionRe: Gradient for MDI clientarea Pin
Ilíon16-Feb-09 19:02
Ilíon16-Feb-09 19:02 
GeneralRe: Gradient for MDI clientarea Pin
Ilíon18-Feb-09 6:10
Ilíon18-Feb-09 6:10 
GeneralMDI client can also be put into other containers, it seems... [modified] Pin
supercat931-Jul-08 14:36
supercat931-Jul-08 14:36 
I just tried putting an MdiClient into a SplitPanel control (find the MDIClient object and set its parent to the appropriate panel of the control). That could be a nice way to allow resizable docked containers (use a SplitPanel, or possibly nested SplitPanels, instead of docked containers; one side of the innermost SplitPanel will hold the MdiClient).

Unfortunately, the only way I can find to add child windows to the SplitPanel is to make the MdiClient's parent object be the form, then set the child's MdiParent to the form, and then set the MdiClient object back to being the SplitPanel. That causes some rather annoying flicker.

modified on Thursday, July 31, 2008 9:23 PM

GeneralAlso see... Pin
Ravi Bhavnani31-Jul-08 9:40
professionalRavi 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.