I have a problem in WinForms. I created a MDIParent-Form and i call a ChildForm from the Load of the MDIParent. And I want that if the ChildForm closes, the MDIParent must close and the Application exits. Thats why i wrote an event for the childForm in the MDIParent, so that if the ChildForm closes the FormClosed-Event would be fired in the MDIParent, but it throws a stack overflow exception. I know that there is a infinite loop, but I dont know why...
private void MDIParent1_Load(object sender, EventArgs e)
{
Form1 childForm = new Form1();
childForm.MdiParent = this;
childForm.FormClosed += childForm_FormClosed;
childForm.Show();
}
void childForm_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
//{Cannot evaluate expression because the current thread is in a stack overflow state.}
}
but if i use
Application.Exit();
instead of this.Close()... everything works fine ... i want to know why...can someone explain??
Update: I have tried the same without a MDIParent and everything works...but why is there a problem if I use a MDIParent
this.Close(), it calls close on all the child forms, which in turn callschildForm_FormClosedand that's your infinite recursion? I'm almost certain Memphy's explanation is correct.