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 in this.Controls)
{
if(ctrl.GetType() == typeof(MdiClient))
{
ctrl.ControlAdded += new ControlEventHandler(this.MDIClient_ControlAdded);
ctrl.ControlRemoved += new ControlEventHandler(this.MDIClient_ControlRemoved);
break;
}
}
protected void MDIClient_ControlAdded(object sender, ControlEventArgs e)
{
Form childform = e.Control as Form;
Trace.WriteLine(String.Concat(childform.Text, ' - MDI child form was added.'));
}
protected void MDIClient_ControlRemoved(object sender, ControlEventArgs e)
{
Trace.WriteLine(String.Concat(e.Control.Text, ' - MDI child form was removed.'));
}
Share with