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
.
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.
If you want to close the form then use Me. Close() instead. The Load event will fire again when you create the new instance. You'll have to change a setting to ensure that doesn't also close your application.
Set the CancelButton property of the form to that button. Gets or sets the button control that is clicked when the user presses the Esc key. You will also have to set the KeyPreview property to true.
The event can be canceled by setting the Cancel property to true. Inside the FormClosing event.
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.
My solution is also in the form properties:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With