detecting in form1 that form2 is closed

c#, forms

Solution

Adhere to the `FormClosed` event of form2.

Wherever you create it do:

form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);

Then create the method:

void form2_FormClosed(object sender, FormClosedEventArgs e)
{
    // Do whatever you want here
}

You'll also want to use `.Show()` instead of `.ShowDialog()` if you want to be able to use either form, otherwise `form1` will be unavailable until `form2` is closed (which I am assuming is not the behavior you are looking for).

Problem

I am trying to detect that form2 is closed in form1. I have this so far ``` private void AddStageBtn_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.ShowDialog(); if (form2.IsDisposed) { MessageBox.Show("it was closed!"); } } ``` Any suggestions? Thanks again!

Original source