0

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.

SQL Police
  • 4,127
  • 1
  • 25
  • 54
  • It's all of this your code? Couldn't you just add a propery `IsClosed` and set it to true in your `FormClosing` if it closes and then check it? – Erik Philips Jun 04 '16 at 23:51
  • http://stackoverflow.com/questions/3861602/how-to-check-if-a-windows-form-is-already-open-and-close-it-if-it-is – prospector Jun 04 '16 at 23:59
  • 3
    Subscribe to its [`FormClosed`](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed(v=vs.110).aspx) event. – CodeCaster Jun 05 '16 at 00:19

1 Answers1

2

You can check the IsHandleCreated property.

frm.Close();
if(frm.IsHandleCreated)
{
    // Closing the form was cancelled, the form is still there
}
NineBerry
  • 26,306
  • 3
  • 62
  • 93