6
Answers

Problem with Closed Event of a Child form

Monte

Monte

13y
3.8k
1
I have an application which has a Parent form that can open multiple instances of a child form. I have used an arraylist to know how many child forms are open. My problem is that if the first child form opened is not the last one to be closed, the closed event does not always fire for that child form.

If the first child form to be opened is the last one to be closed then the closed event fires every time.

I know how many forms are open by using a counter = "childFormOpenCount" - for commissioning purposes I am displaying this number in a messagebox every 10 seconds. I also display a messagebox "Closing Mimic" whenever the childform closed event is fired - as in the code below.

I know the Close event has not fired by the messagebox "Closing Mimic" not being displayed and the childFormOpenCount not being decremented.

To close the child form I have tried using the "X" at the top of the Child Form (now disabled in the project)  and an "Exit" button that I added with a click event containing the code:

this.Close();

Both methods of closing the child Form give the same problem results.

I am using Visual Studio 2003 (not possible to use a later version).

I have pasted the relevant sections of code (maybe in a reply to this post as I think otherwise too long) and uploaded the project.

Thanks in advance for any help...










#region Method CreateNewMimic
    private void CreateNewMimic()
    {
      try
      {
        //Create new instance of Mimic form along with the fields that are fixed and   //wont change
        //NB only need Channel ID, Name & channel number as these will not change
        this.ChildMimicForm = new   MimicForm(mimicFormAnalogueChannelID,mimicFormAnalogueName,mimicFormAnalogueChannelNumInt,mimicFormAnalogueUnits);
      
        //Add an event handler in Parent form when this new Child form is closed
        this.ChildMimicForm.Closed += new System.EventHandler(this.ChildMimicForm_Closed);
    
        //Append this MimicForm to arraylist of Forms for storage
        childMimicFormArrayList.Add(this.ChildMimicForm);

        //Increment Counter for number of Child forms open
        childFormOpenCount ++;

        //Increment Form Array index ready for the next MimicForm to be opened & then   //stored
        childMimicFormArrayListIndex ++;

        //Append this current Channel Number (entered for this Form) into array of   //integers
        childMimicFormChannelNumArrayList.Add(mimicFormChannelNumWithOffset);
      
        //Increment Channel Number Array index ready for the next MimicForm Channel   //number to be entered & then stored
        childMimicFormChannelNumArrayListIndex ++;

        //Open ChildForm
        this.ChildMimicForm.Show();

      }

      catch (Exception e)
      {
      MessageBox.Show("Error in Method 'CreateNewMimic:' " + e.Message);
      }
    }

    #endregion


    #region Event handler when Child Mimic Form Closed
    private void ChildMimicForm_Closed(object sender, EventArgs e)
    {
      //This childform event was created manually above when this version of childform   //was opened

      //Decrement Counter for number of Child forms open
      childFormOpenCount --;

      MimicForm ChildMimicForm = (MimicForm)sender;

      //Message for commissioning use
      MessageBox.Show("Closing Mimic","Commissioning");
    
      //Remove event handler for this version of Child Form
      this.ChildMimicForm.Closed -= new System.EventHandler(this.ChildMimicForm_Closed);

      //Remove this version of Child Form from Array List - to save resources
      childMimicFormArrayList.Remove(ChildMimicForm);

    
    
    }

    #endregion

Answers (6)