Dialog: Cancel button / esc key (Windows forms)

W

WP

Hello, a short question: Do I have to add a cancel button to my form
dialog for it to be closable by the esc key? Right now I have no
cancel button in my dialog and I noticed the esc key will not close
it. First I thought the two textboxes I have in the dialog "consumed"
the esc key press but I removed them and the dialog still doesn't
close when the esc key is pressed so that was not it.

- Eric
 
P

Peter Duniho

Hello, a short question: Do I have to add a cancel button to my form
dialog for it to be closable by the esc key?

That's probably the easiest way. But you can always handle the key
yourself in your Form class. So, no...you don't _have_ to add a cancel
button.

Pete
 
T

twoj wladca

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)27)
Application.Exit();
}
 
W

WP

That's probably the easiest way. But you can always handle the key
yourself in your Form class. So, no...you don't _have_ to add a cancel
button.

Pete

Thanks for your quick and helpful reply, Pete! I added a button
labelled "Cancel" and set its DialogResult property to
DialogResult.Cancel. I also had to set the form's CancelButton
property to this key. I also set the forms AcceptButton property to my
"OK"-button. Very convenient. Thanks again!

I'm thinking about disabling the OK-button until the two textboxes I
have in the dialog have input inside them, but maybe I should just
check that in OnClosing() (because I know how to do that, heh). I
don't know how to get notifications about text changes in the text
both in the form and enabling/disabling the OK-button accordingly.

- Eric
 
P

Peter Duniho

[...]
I'm thinking about disabling the OK-button until the two textboxes I
have in the dialog have input inside them, but maybe I should just
check that in OnClosing() (because I know how to do that, heh). I
don't know how to get notifications about text changes in the text
both in the form and enabling/disabling the OK-button accordingly.

Apropos of my previous comments, this is a very good example of what I
mean. I recommend you take a look at the events in the TextBox class and
see if any of them appear to have anything to do with "text changes". :)

Pete
 
P

Peter Duniho

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)27)
Application.Exit();
}

If you are going to follow this technique, Eric (and it seems from your
other post you're not), I'd recommend handling instead the KeyDown event,
and comparing against Keys.Escape rather than the above.

Noting also, of course, that for simply closing a dialog box, calling
Application.Exit() is probably overkill. :)

Pete
 
W

WP

[...]
I'm thinking about disabling the OK-button until the two textboxes I
have in the dialog have input inside them, but maybe I should just
check that in OnClosing() (because I know how to do that, heh). I
don't know how to get notifications about text changes in the text
both in the form and enabling/disabling the OK-button accordingly.

Apropos of my previous comments, this is a very good example of what I
mean. I recommend you take a look at the events in the TextBox class and
see if any of them appear to have anything to do with "text changes". :)

Pete

Thanks, I found the event and added a handler for both text boxes (the
same for both) and it works great! Really pleased with it. I'm trying
to limit the ways in which invalid or missing user input can be
supplied (or not supplied in case of missing) somewhat at least.

- Eric
 
W

WP

If you are going to follow this technique, Eric (and it seems from your
other post you're not), I'd recommend handling instead the KeyDown event,
and comparing against Keys.Escape rather than the above.

Noting also, of course, that for simply closing a dialog box, calling
Application.Exit() is probably overkill. :)

Pete

No, I am not going down that road even though I appreciate people
trying to help me. But I can't help thinking that that code looks like
my very simple key handlers I wrote in pure C when doing glut-
programming. :)
 

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