tags:

views:

97

answers:

2

How can I find out if a form is closed by clicking the X button or by (this.Close())?

+9  A: 

the form has the event FormClosing with parameter of type FormClosingEventArgs.

private void Form1_FormClosing( object sender, FormClosingEventArgs e )
{
    if ( e.CloseReason == CloseReason.UserClosing )
    {
        if ( MessageBox.Show( this, "Really?", "Closing...",
             MessageBoxButtons.OKCancel, MessageBoxIcon.Question )
            == DialogResult.Cancel ) e.Cancel = true;
    }
}
ibram
I don't want to ask if Really Close. My Form has a Cancel button and at clicking Cancel I set to null a field that will be returned. From outside, I know that I don't have to do something when this form returns null. But when the form is closed by clicking X, the field is not null so the outside code crashes.
fm_strategy
Technically he did answer your question...
Xander
A: 

You could remove the 'X' altogether?

One of the properties of the form is "ControlBox" just set this to false

Xander