Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detecting in form1 that form2 is closed

Tags:

c#

forms

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!

like image 383
Badmiral Avatar asked Nov 01 '25 18:11

Badmiral


1 Answers

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).

like image 118
NominSim Avatar answered Nov 03 '25 09:11

NominSim