How do you check for a close event for an MDI child form when clicking the "X" button and let the parent form know that it has closed?
MDI Child Forms C#
1.8k Views Asked by NexAddo At
4
There are 4 best solutions below
2

In the form FormClosing event you can do
TheMainForm form = (TheMainForm)this.MdiParent()
form.AlertMe( this );
0

Attach a closed event to the childform from within the mainForm
Form mdiChild = new Form();
mdiChild.MdiParent = this;
mdiChild.Closed += (s, e) => { //... };
mdiChild.Show();
didn't check the code but should not be that hard
0

well, The below code shows how parent form recognises whether the child form has been closed or not and it can also recognises that is there any new child form is added to that parent form..
private List<Form> ChildFormList = new List<Form>();
private void MyForm_MdiChildActivate(object sender, EventArgs e)
{
Form f = this.ActiveMdiChild;
if (f == null)
{
//the last child form was just closed
return;
}
if (!ChildFormList.Contains(f))
{
//a new child form was created
ChildFormList.Add(f);
f.FormClosed += new FormClosedEventHandler(ChildFormClosed); // here the parent form knows that that child form has been closed or not
}
else
{
//activated existing form
}
}
private void ChildFormClosed(object sender, FormClosedEventArgs e)
{
//a child form was closed
Form f = (Form)sender;
ChildFormList.Remove(f);
}
You can simply listen to the FormClosed event in the MDI.