CancelButton & ShowDialog

  • Thread starter emorgoch.public
  • Start date
E

emorgoch.public

Hello there,

I have a form that gets shown using the ShowDialog() method. This form
then has it's accept and cancel button properties set. If hte user hits
the cancel button / hits ESC, the form is closed and they're returned
to the parent form. If the use then clicks the accept button / hits
enter, a different part of the form become enabled for them to work
with. Here's where I'm running into a bit of a problem:

When I enable this new part of the form, I also change the forms Accept
& Cancel button properties to a different set of buttons to save /
cancel data changes. If the user hits the second cancel button, no
changes occur, the new part of the form becomes disabled again, and the
control focus is returned to first part of the form (which was also
disabled when the second part was enabled).

However, in my testing, if the user hits the ESC key, the form is
closed no matter what is in the button event handler. From my trial and
error testing, it seems that this is a global property of any dialog
that is displayed using ShowDialog, and has it's CancelButton property
set.

Now, I've coded up a work around for this by having the cancel button
set a form variable, and then writing a Closing event handler to check
to see if that variable is set. But that seems like a poor way of doing
it, and that there should be another option.

Any suggestions / comments? Thanks
 
C

Chris Jobson

Hello there,

I have a form that gets shown using the ShowDialog() method. This form
then has it's accept and cancel button properties set. If hte user hits
the cancel button / hits ESC, the form is closed and they're returned
to the parent form. If the use then clicks the accept button / hits
enter, a different part of the form become enabled for them to work
with. Here's where I'm running into a bit of a problem:

When I enable this new part of the form, I also change the forms Accept
& Cancel button properties to a different set of buttons to save /
cancel data changes. If the user hits the second cancel button, no
changes occur, the new part of the form becomes disabled again, and the
control focus is returned to first part of the form (which was also
disabled when the second part was enabled).

However, in my testing, if the user hits the ESC key, the form is
closed no matter what is in the button event handler. From my trial and
error testing, it seems that this is a global property of any dialog
that is displayed using ShowDialog, and has it's CancelButton property
set.

Now, I've coded up a work around for this by having the cancel button
set a form variable, and then writing a Closing event handler to check
to see if that variable is set. But that seems like a poor way of doing
it, and that there should be another option.

Any suggestions / comments? Thanks

I don't know why your form doesn't close when the second cancel button is
pressed, because in all my testing I've found that pressing ESC has exactly
the same effect as clicking the form's CancelButton (including executing
that button's event handler). If your event handler is getting executed when
ESC is pressed then an easy way to prevent the form closing is to add the
line "DialogResult = DialogResult.None;" to the event handler.

Chris Jobson
 
A

Ahmed

Hi,

The standard for save is not the enter button. Why don't you use
shortcuts for saving, like ctrl + S or Alt + S? and for cancel you can
use Alt + C. Regarding the global ESC you can handle the form closing
event and check either a global variable or a disabled/enabled control.

There might be other solutions.

Ahmed
 
K

Kevin Spencer

The behavior of the CancelButton property is to close the Form and set the
DialogResult to DialogResult.Cancel. This is not changeable, as contrasted
with the AcceptButton, which will set the DialogResult of the Form to
whatever you tell it to. So, if you don't want the ESCAPE key to close the
form, don't set the CancelButton property, but handle the keydown or
keypress event instead.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Nyuck nyuck nyuck
 

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