Question

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?

Was it helpful?

Solution

You can simply listen to the FormClosed event in the MDI.

var childForm = new ChildForm();
childForm.FormClosed += new FormClosedEventHandler(form_FormClosed);
childForm.Show();

OTHER TIPS

In the form FormClosing event you can do

TheMainForm form = (TheMainForm)this.MdiParent()
form.AlertMe( this );

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

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top