Is there an event I can use to tell if a child form has been added or removed from the MDI parent?
Since the MdiChildActivate event is fired before the MDI child form is actually closed and therefore there isn't enough information to detect if a MDI child form is actually activated or closed, I chose a different approach to solve the problem.
I found that the ParentChanged event fires on the MDI child form when it is closed.
public class MdiParentForm : Form
{
// ...
private Form CreateMdiChildForm()
{
var form = new MdiChildForm
form.ParentChanged += MdiFormParentChangedHandler;
form.MdiParent = this;
return form;
}
private void MdiFormParentChangedHandler(object sender, EventArgs args)
{
var form = sender as Form;
if (form != null)
{
if (form.MdiParent != null)
{
// MDI child form will activate
// ... your code here
}
else
{
// MDI child form will close
form.ParentChanged -= MdiFormParentChangedHandler;
// ... your code here
}
}
}
// ...
}