Live Chat Icon For mobile
Live Chat Icon

I need to perform certain custom processing whenever an MDI child form is added to/removed from the MDIContainer form. How do I determine this?

Platform: WinForms| Category: MDI

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

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.