How to Cancel a Cancel Button event

E

Eric T.

When the user presses Cancel on my form, I want there to be an opportunity to
cancel the cancel, i.e. "Do you really want to abort the changes you've
made". If the user presses N, I don't want it to exit. If the user presses
Y, I set the DialogResult to Cancel and call this.Close(). I don't do
anything if the user presses N, but the form closes anyways. How do I stop
the form from closing?
Thanks
PS - The cancel button object is registered in the form as the CancelButton.
 
M

miher

Hi,

You can use the Form.FormClosing event, to cancel the close, just set the
FormClosingEventArgs parameters Cancel property to true.

Hope You find this useful.
-Zsolt
 
H

Herfried K. Wagner [MVP]

Eric T. said:
When the user presses Cancel on my form, I want there to be an opportunity
to
cancel the cancel, i.e. "Do you really want to abort the changes you've
made". If the user presses N, I don't want it to exit. If the user
presses
Y, I set the DialogResult to Cancel and call this.Close(). I don't do
anything if the user presses N, but the form closes anyways. How do I
stop
the form from closing?

Just do not assign a 'DialogResult' to the button's 'DialogResult' property
at design time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top