SQL Police
SQL Police

Reputation: 4196

After calling Close(), how to check if the form was really closed?

I have a custom Form class, which is reacting to the Closing event, like this:

class MyForm: Form
{
    ...
    private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if ( ... )
            e.Cancel = true;
    }    
    ...  
}

Now, somewhere else, I call Close() on this form:

 MyForm frm;
 ...
 frm.Close();

Question: After calling Close(), how can I find out whether the form was really closed, or if the closing was cancelled in the Closing Event?

The Close() method does not return a value, and it also does not throw an exception.

Upvotes: 1

Views: 1361

Answers (1)

NineBerry
NineBerry

Reputation: 28499

You can check the IsHandleCreated property.

frm.Close();
if(frm.IsHandleCreated)
{
    // Closing the form was cancelled, the form is still there
}

Upvotes: 3

Related Questions