Cancel form closing

  • Thread starter Thread starter Patrick Kristiansen
  • Start date Start date
P

Patrick Kristiansen

Hi!

When setting e.Cancel = true in a forms Closing event, you can prevent
the form being closed. I did so, but on my form I have a button with
DialogResult set to Abort, but the form doesn't close now that I've set
Cancel to true.

How can I work around this so that you cannot close the form by using
Alt-F4 but still allow the controls to call the Close method of the form?

Thanks in advance,
Patrick
 
* Patrick Kristiansen said:
When setting e.Cancel = true in a forms Closing event, you can prevent
the form being closed. I did so, but on my form I have a button with
DialogResult set to Abort, but the form doesn't close now that I've
set Cancel to true.

How can I work around this so that you cannot close the form by using
Alt-F4 but still allow the controls to call the Close method of the
form?

Remove the 'DialogResult' from the button and add this code in its
'Click' event handler:

\\\
m_Close = True
Me.DialogResult = DialogResult.Abort
Me.Close()
///

On top of the code you add:

\\\
Private m_Close As Boolean
///

In the 'Closing' event handler you check 'm_Close' and set 'e.Cancel' to
'True' only if it is not set.
 
Back
Top