how can i enable the escape key function to close a form?
Printable View
how can i enable the escape key function to close a form?
If you have a Cancel button that closes the form, just set that buttons Cancel property to true. The Escape will then trigger the Cancel-buttons clickevent.
if not u can use this code in ur form,
Code:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'27 is the keycode for the escape key
If KeyCode = 27 Then End
End Sub
But will that trigger if you are in a control on the form?
Set the form's KeyPreview property to True and it will work, even if you have a control selected, it will still work.Quote:
Originally posted by Thomas Halsvik
But will that trigger if you are in a control on the form?
I can't say it too often, don't use end to stop your program, it's a rude way to quit your program.
Use Unload Me to quit your program.
Code:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'VBKeyEscape is easier to follow then 27
If KeyCode = vbKeyEscape Then
Unload Me
End If
End Sub