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
Loading
Praneeth KumarPosted Jun 17, 2011, 8:16 AM
It will work as i tried it and it works. So next question remains is where you want to remove the event handler, which I am still investigating on.
MontePosted Jun 22, 2011, 4:02 PM
As suggested, I commented out the line which deletes the Closed event for the Child form and it worked fine.
Looks like something in VS2003 gets confused as to which closed event delete... Below is my modified code for the closed event. I still have the closed event which still fires when the child form is closed. I have got rid of the counter that counts everytime a child form is opened/closed.
#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
MimicForm ChildMimicForm = (MimicForm)sender;
//Messagebox displayed when Child Mimic Form Closed - for commissioning use only
//MessageBox.Show("Closing Mimic","Commissioning");
//Remove event handler for this version of Child Form
//*****This has been commented out as otherwise the closed events do not always trigger correctly *****
//***** ie this closed event does not fire correctly and hence the child form does not get removed *****
//***** from the arraylist. This code has been left commented out to show that it shouldnt be used. *****
//this.ChildMimicForm.Closed -= new System.EventHandler(this.ChildMimicForm_Closed);
//Get the index number of the ChildMimicForm in array list
int index = childMimicFormArrayList.IndexOf(ChildMimicForm);
//Messagebox to show current index number and arry contents at this location - for commissioning use only
//MessageBox.Show ("Index # = " + index + ", Contents = " + childMimicFormChannelNumArrayList[index].ToString(), "Commissioning");
//Remove channel number from the arraylist for the deleted childform - to save resources
childMimicFormChannelNumArrayList.RemoveRange(index,1);
//Remove deleted childform from Array List - to save resources
childMimicFormArrayList.Remove(ChildMimicForm);
}
MontePosted Jun 21, 2011, 7:22 AM
Praneeth KumarPosted Jun 21, 2011, 6:32 AM
I could not find time to resolve it. By any chance you fixed it?
Regards,
Praneeth
Praneeth KumarPosted Jun 17, 2011, 9:13 AM
MontePosted Jun 17, 2011, 9:01 AM