wawaragna
wawaragna

Reputation: 21

FormClosing in c#

Good day stackoverflow. My problem here is that the close form function is not implemented right after the closure of the form. But if I close the form and open it up again and close it again the Close_Form function executes. How can I do it in such a way that after compile and run the program I could use the close form method right away? Please help.tnx

private Form2 ins = new Form2();

private void userManageLink_Click(object sender, EventArgs e)
    {


        ins.ShowDialog();

        ins.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Close_Form);

    }

    private void Close_Form(object sender, EventArgs e)
    {

        MessageBox.Show("Hello World");

    }

Upvotes: 2

Views: 425

Answers (2)

groundh0g
groundh0g

Reputation: 406

The problem is likely that you're adding the FormClosing handler after the ShowDialog call. The dialog is shown, you close it, then you add the handler. Try adding the handler before the ShowDialog call.

Upvotes: 1

siride
siride

Reputation: 209945

Move the assignment of the event handler to before the call to ShowDialog(). Once you call ShowDialog(), it doesn't return until the form has already closed. By the time, there's no point in installing the event handler because the form is already closed and the event won't be fired.

Upvotes: 6

Related Questions