Frage

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.

War es hilfreich?

Lösung

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
    }
}

Andere Tipps

In the form closing event set the DialogResult as follows:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    this.DialogResult = System.Windows.Forms.DialogResult.Yes;
}

When you open the form look for the response as follows:

if (Form1.ShowDialog() == DialogResult.Yes)
{
    ////do stuff.
}

I hope this helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top