I\'m attempting to detect, on the MDI parent, when my MDI child form closes, and react accordingly. The MDI parent shouldn\'t do anything until the MDI child closes. Here is
When mark the ParentForm as MdiContainer by setting the IsMdiContainer to true, the ParentForm.ControlAdded event raised for adding the "MdiClient" control to the parent form. So when adding MdiClient to parent MDI form, we can raise the ControlAdded event for the MdiClient control as like below,
public ParentForm()
{
InitializeComponent();
this.ControlAdded += Form1_ControlAdded;
this.IsMdiContainer = true;
We need to raise the MdiClient.ControlAdded as like the below,
void Form1_ControlAdded(object sender, ControlEventArgs e)
{
if(e.Control is MdiClient)
e.Control.ControlAdded += MdiClient_ControlAdded;
}
By default the MDI Child forms are added into the controls collection of the MdiClient in Parent form. So when set the ChildForm.MdiParent value as Parent form, the ControlAdded event for the MdiClient will raise.
void MdiClient_ControlAdded(object sender, ControlEventArgs e)
{
if (e.Control is Form)
{
var form = e.Control as Form;
form.FormClosing += form_FormClosing;
form.FormClosed += form_FormClosed;
}
}
In this above MdiClient_ControlAdded method raises when child form added into the Parent MDI form. So by raising the FormClosing and FormClosed events for child forms you can easily detect whether the child form is closed or not.