The default behavior is to make the client container use the Control color from the Control panel. You can change this behavior by making the MDI Client container use the form’s BackColor and Image. To do this, after the call to InitializeComponents(), add the code below. You can also download a working MDI Client project that has this code in it.
//set back color
foreach(Control c inthis.Controls)
{
if(c is MdiClient)
{
c.BackColor = this.BackColor;
c.BackgroundImage = this.BackgroundImage;
}
}
In .Net 1.0, the child forms do not get the Form.Activated event (only the parent MDI). To catch MDI children being activated, listen to the Enter/Leave events of that child Form or listen to the Form.MdiChildActivate event in the parent Form.
In 1.1 the child Forms do get the Activated event.
Here is how it can be done. This takes into account all docked controls (including menus) in the mdi parent form.
[C#]
privatevoidFillActiveChildFormToClient()
{
Form child = this.ActiveMdiChild;
Rectangle mdiClientArea = Rectangle.Empty;
foreach(Control c inthis.Controls)
{
if(c is MdiClient)
mdiClientArea = c.ClientRectangle;
}
child.Bounds = mdiClientArea;
}
[VB.Net]PrivateSub FillActiveChildFormToClient()Dimchild As Form = Me.ActiveMdiChild DimmdiClientArea As Rectangle = Rectangle.Empty Dimc As ControlForEach c In Me.ControlsIfTypeOf c Is MdiClient ThenmdiClientArea = c.ClientRectangleEndIfNextchild.Bounds = mdiClientAreaEndSub
You should not try listening to your MDI container Form’s Paint event, instead listen to the Paint event of the MDIClient control that is a child of the mdi container form. This article provides you a detailed example:
It appears that this behavior is a bug that will be corrected in a future .NET release.
You can control the size of your child form by adding a Layout event handler for it. Here is a code snippet that imposes the minimum size that you set in its properties. You can also handle it by overriding the form’s WndProc method as explained in this Microsoft KB article.
[C#]
private void Document_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
{
if(this.Bounds.Width < this.MinimumSize.Width)
this.Size = new Size(this.MinimumSize.Width, this.Size.Height);
if(this.Bounds.Height < this.MinimumSize.Height)
this.Size = new Size(this.Size.Width, this.MinimumSize.Height);
}
[VB.NET]
Private Sub Document_Layout(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles MyBase.Layout
If (Me.Bounds.Width < Me.MinimumSize.Width) Then
Me.Size = New Size(Me.MinimumSize.Width, Me.Size.Height)
End If
If (Me.Bounds.Height < Me.MinimumSize.Height) Then
Me.Size = New Size(Me.Size.Width, Me.MinimumSize.Height)
End If
End Sub
MDIContainer forms have an MDIClient child window and it is to this MDIClient window that MDI child forms are parented. The MDIClient’s ControlAdded/ControlRemoved events will be fired whenever a child form is added or removed. You can subscribe to these events and add the required processing code from within the handlers.
// From within the MDIContainer form, subscribe to the MDIClient’s ControlAdded/ControlRemoved events foreach(Control ctrl inthis.Controls)
{
if(ctrl.GetType() == typeof(MdiClient))
{
ctrl.ControlAdded += new ControlEventHandler(this.MDIClient_ControlAdded);
ctrl.ControlRemoved += new ControlEventHandler(this.MDIClient_ControlRemoved);
break;
}
}
protectedvoidMDIClient_ControlAdded(object sender, ControlEventArgs e)
{
Form childform = e.Control as Form;
Trace.WriteLine(String.Concat(childform.Text, ' - MDI child form was added.'));
}
protectedvoidMDIClient_ControlRemoved(object sender, ControlEventArgs e)
{
Trace.WriteLine(String.Concat(e.Control.Text, ' - MDI child form was removed.'));
}
The first time this code is called, the static GetInstance method will create and return an instance of the child form. Every other time this code is called, the GetInstance method will return the existing instance of the child from, stored in the static field childForm. If the child form instance is ever destroyed, the next time you call GetInstance, a new instance will be created and then used for its lifetime.
Also, if you need constructors or even overloaded constructors for your MDI Child Form, just add the needed parameters to the GetInstance function and pass them along to the class constructor.