In my winform application I have one form that creates other forms during a for loop. This parent form remains hidden is there purely for processing.
I moved the processing code away from the program class because Application.Run
didn't seem to place nice in a loop as more than one instance would be open.
When the child forms are done with they are closed. What I want to know is whether I can get the application to exit when these forms are closed even though the parent form is still open. I have tried exposing a List<bool>
on the parent form to store which of the forms have closed but the child forms cannot access the list due to the parent form not having an instance name - it is called by Application.Run(new FormProcessor(args));
More generally I guess I am asking is there a way to access properties of the parent form from the child forms.
Thanks to all who helped with this. I have come up with an answer that works for me. I've added an event handler on the back of form closed. The origList
variable stores the original data for formList
otherwise the foreach
will proceed to the next entry of the list which it may have removed.
for ( int i =0; i < formList.Count; i++)
{
string formName = formList[i];
Form1 frm = (new Form1(formName...);
frm.Show();
string contextName= formName;
frm.FormClosed += new FormClosedEventHandler((sender, e) => FrmClosed_Event(sender, e, contextName));
}
public void FrmClosed_Event(object sender, FormClosedEventArgs e, string name)
{
foreach(string thisForm in origList)
{
if (thisForm == name)
{ formList.Remove(thisForm); }
}
if (formList.Count == 0)
{ Application.Exit(); }
}