Search code examples
c#formclosingactive-form

Invoke event handler on a different form after closing the active form


I have a C# application in which I need specific or a function to execute on a form after closing the active form.

The form in which I need the code to excute becomes the active form after the previous active form is closed. So in a nutshell after closing this form the form in which I need the event handler or function to run will then become the active form. Is there a way that this is possible?

I have tried the Form_Enter event handler on the form that becomes active after the other form is closed, but that did not work.


Solution

  • I believe you can achieve what you are trying to do more simply. The code you use to show Form2 from Form1 (Main), you can add your code there like so:

    Class Form1 {
        private void button_click(object sender, EventArgs e) {
            Form2  newForm = Form2();
            newForm.ShowDialog(); // To prevent the main form carry on
            // Your code needed to be excuted
        }
    }