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
2010-09-17 13:43:32
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
2010-09-17 14:09:20
Technically he did answer your question...
Xander
2010-09-17 14:58:22
A:
You could remove the 'X' altogether?
One of the properties of the form is "ControlBox" just set this to false
Xander
2010-09-17 14:57:30