Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms; detecting form closed from another form

Tags:

c#

forms

winforms

Is it possible to detect a form closing from another form. For example. If I had a mainForm that opens subForm, can I detect within the mainForm that the subForm has closed and execute code?

I understand I could create an event handler within the subForm, but this is not really what I'm after because what I'm about to do after the subForm closes, is within the mainForm (changes to mainForm).

like image 716
K. Sunki Avatar asked May 26 '17 14:05

K. Sunki


Video Answer


1 Answers

The FormClosed event is public, so you can create a handler from the main form.

//Inside main Form.  Click button to open new form
private void button1_Click(object sender, EventArgs e)
{
      Form2 f2 = new Form2();
      f2.FormClosed += F2_FormClosed;
      f2.Show();
}

private void F2_FormClosed(object sender, FormClosedEventArgs e)
{
    MessageBox.Show("Form was closed");
}
like image 101
jason.kaisersmith Avatar answered Nov 15 '22 00:11

jason.kaisersmith