Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Dialog: Cancel button / esc key (Windows forms)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WP - 14 Mar 2008 22:13 GMT
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
Peter Duniho - 14 Mar 2008 22:28 GMT
> 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
WP - 14 Mar 2008 22:46 GMT
> > 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?
[quoted text clipped - 4 lines]
>
> 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
Peter Duniho - 14 Mar 2008 23:17 GMT
> [...]
> 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
WP - 14 Mar 2008 23:40 GMT
> > [...]
> > I'm thinking about disabling the OK-button until the two textboxes I
[quoted text clipped - 8 lines]
>
> 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
twoj wladca - 14 Mar 2008 22:36 GMT
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (e.KeyChar == (char)27)
               Application.Exit();
       }

> 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
[quoted text clipped - 4 lines]
>
> - Eric
Peter Duniho - 14 Mar 2008 23:18 GMT
>         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
WP - 14 Mar 2008 23:42 GMT
> >         private void Form1_KeyPress(object sender, KeyPressEventArgs e)
> >         {
[quoted text clipped - 10 lines]
>
> 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. :)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.