15

I'm using VB 2010 Express.

In C# I would set the forms CancelButton property.

For this VB form I don't have a CancelButton so I suspect I need to program either KeyPress or KeyDown.

  1. What is the difference between these two events?
  2. Which should I use?
  3. I assume the general code for this is as follows?:

    If e.KeyCode = Keys.Escape Then
        Close()
    End If
    

I have certain .Focus code within other controls of the form then it becomes pointless putting this in the main forms event procedure as the main form never really has the focus.

whytheq
  • 34,466
  • 65
  • 172
  • 267
  • add the code below, Remember to set the KeyPreview property on the form to TRUE. – Patrick Guimalan Nov 22 '12 at 09:25
  • 2
    [`Form` has a `CancelButton`](http://msdn.microsoft.com/en-us/library/system.windows.forms.form.cancelbutton.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2) property whatever language you are using - I'm not sure what you mean by "For this VB form I don't have a CancelButton" – AakashM Nov 22 '12 at 09:37
  • @AakashM ...thanks - it was there all along!! (teach me for posting at 730am in the morning) – whytheq Nov 22 '12 at 14:19
  • @AakashM I'm still curious about the difference between `KeyPress` and `KeyDown` ? – whytheq Nov 22 '12 at 14:20
  • sir, i think you have to read this http://stackoverflow.com/questions/1367700/whats-the-difference-between-keydown-and-keypress-in-net – Patrick Guimalan Nov 22 '12 at 14:42
  • 1
    From [MSDN](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx) "The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events." So, different behaviour for example between `A` and `SHIFT` – AakashM Nov 22 '12 at 14:49

4 Answers4

36

Set your form keydown to

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.Escape Then Me.Close()
End Sub

Then, Remember to set the KeyPreview property on the form to TRUE.

Patrick Guimalan
  • 990
  • 9
  • 11
3

My solution is also in the form properties:

  • set the CancelButton Property to button that performs your cancel function
My Brain
  • 41
  • 4
2
  1. You say "the VB form has no Cancel Button" -- so give it an invisible one. a.) Add the button to the form. b.) Set it's Visible property to False. (Remember, computers don't make mistakes -- they lie. So let's get even.)
  2. On the form properties menu, set the CancelButton Property to your invisible cancel button.
  3. On the form properties menu, set the KeyPreview Property to True.
  4. Double click on your invisible CancelButton and add Me.Dispose()
  5. Now you have a Micro-Nasty mess to deal with. One of the basic principles of structured programming is that "there must be only one exit from a routine." You don't want to handle an exit routine in two different places. And your user can get out by either using "Esc" in the upper right hand corner. The heck with structured programming. So . . .
  6. The upper right black "X" on red button calls FormName_Deactivate -- if the name of your form is FormSanta then it is FormSanta_Deactivate. For simplicity's sake, never mind the right way, (Micro-serfs in your velvet sweat-shop, can you hear me crying?) give your upper right "X" button and your invisible exit button the same code. Then add on Me.Close()
0

"KeyPreview" Property of the form has to be set to true, or this will not work...

Kevin Henzel
  • 155
  • 1
  • 2
  • 14