Reputation: 772
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?
Upvotes: 0
Views: 1898
Reputation: 17701
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);
}
Upvotes: 0
Reputation: 18577
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
Upvotes: 0
Reputation: 31448
You can simply listen to the FormClosed event in the MDI.
var childForm = new ChildForm();
childForm.FormClosed += new FormClosedEventHandler(form_FormClosed);
childForm.Show();
Upvotes: 4
Reputation: 3485
In the form FormClosing event you can do
TheMainForm form = (TheMainForm)this.MdiParent()
form.AlertMe( this );
Upvotes: 0