I was working with VB6. Now I am trying to study VB.NET. In vb6 There is Cancel property for command button
eg. If we make the cancel property of exit command button 'True',then if we press Esc key ,we can exit from that form even without clicking the exit button.
On studying the properties of buton in VB.NET I couldn't see the cancel property.Am I right ? or Is there any similar property for the buttons..

Any body please advise me..

4 Years
Discussion Span
Last Post by Start4me

The form has a CancelButton property, so you could do something like:
MyForm.CancelButton = MyCancelButton


You can create a keydown event for the exit button, and insert the code below, but in order to press on the escape button and exit the form, the exit button needs to be in focus.

Private Sub ButtonExit_KeyDown(sender As Object, e As KeyEventArgs) Handles ButtonExit.KeyDown
    Select Case e.KeyCode
       Case Keys.Escape
    End Select
 End Sub

You can bring back the focus to the exit button either when you click on the form:

Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
End Sub

or when you hover with the mouse over the form:

Private Sub Form1_MouseHover(sender As Object, e As EventArgs) Handles Me.MouseHover
End Sub

Edited by Start4me: improve answer

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.