Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I perform custom processing when an MDI child form is added to or removed from the MDIContainer form?

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 Controls ) 
  if ( ctrl.GetType() == typeof( MdiClient ) ) 
  { 
    ctrl.ControlAdded += 
      new ControlEventHandler( MDIClient_ControlAdded ); 
    ctrl.ControlRemoved += 
      new ControlEventHandler( 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." ) ); 
}  

Contributed from George Shepherd's Windows Forms FAQ