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:
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:
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:
PictureBox1.Left = (ClientControl.Width \ 2) - (PictureBox1.Width \ 2)
PictureBox1.Top = (ClientControl.Height \ 2) - (PictureBox1.Height \ 2)
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:
If ClientControl IsNot Nothing Then
ClientControl.Dock = DockStyle.Right
ClientControl.Width = Me.ClientRectangle.Width \ 2
End If
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.