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

Re: Dialog: Cancel button / esc key (Windows forms) by Peter

Peter
Fri Mar 14 16:28:30 CDT 2008

On Fri, 14 Mar 2008 14:13:06 -0700, WP <mindcooler@gmail.com> wrote:

> 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

Re: Dialog: Cancel button / esc key (Windows forms) by twoj

twoj
Fri Mar 14 16:36:51 CDT 2008

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

Uzytkownik "WP" <mindcooler@gmail.com> napisal w wiadomosci
news:397efa4a-6c85-4907-9e03-72eea0493065@i29g2000prf.googlegroups.com...
> 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


Re: Dialog: Cancel button / esc key (Windows forms) by WP

WP
Fri Mar 14 16:46:34 CDT 2008

On 14 Mar, 22:28, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote:
> On Fri, 14 Mar 2008 14:13:06 -0700, WP <mindcoo...@gmail.com> wrote:
> > 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

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

Re: Dialog: Cancel button / esc key (Windows forms) by Peter

Peter
Fri Mar 14 17:17:08 CDT 2008

On Fri, 14 Mar 2008 14:46:34 -0700, WP <mindcooler@gmail.com> wrote:

> [...]
> 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

Re: Dialog: Cancel button / esc key (Windows forms) by Peter

Peter
Fri Mar 14 17:18:52 CDT 2008

On Fri, 14 Mar 2008 14:36:51 -0700, twoj wladca <kriz@vp.pl> wrote:

> private void Form1_KeyPress(object sender, KeyPressEventArgs e=
)
> {
> if (e.KeyChar =3D=3D (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

Re: Dialog: Cancel button / esc key (Windows forms) by WP

WP
Fri Mar 14 17:40:06 CDT 2008

On 14 Mar, 23:17, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote:
> On Fri, 14 Mar 2008 14:46:34 -0700, WP <mindcoo...@gmail.com> wrote:
> > [...]
> > 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

Re: Dialog: Cancel button / esc key (Windows forms) by WP

WP
Fri Mar 14 17:42:23 CDT 2008

On 14 Mar, 23:18, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote:
> On Fri, 14 Mar 2008 14:36:51 -0700, twoj wladca <k...@vp.pl> wrote:
> > 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

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. :)