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..

Dani AI

Generated

Both answers already point to workable approaches but miss a few practical details and pitfalls.

is pointing to the built‑in form mechanism (set the form’s Cancel button in the designer or at runtime). ’s KeyDown-on-the-button approach will work, but it’s brittle because it requires the button to have focus and the sample calls Application.Exit (which terminates the whole app). A more robust pattern is to handle the key at the form level so Escape is caught regardless of which control has focus. Enable key preview and use a form KeyDown handler:

' enable in designer or at runtime
Me.KeyPreview = True

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Escape Then
        e.SuppressKeyPress = True
        ' close this form or trigger your cancel logic
        Me.Close()
    End If
End Sub

For modal dialogs use the button’s DialogResult so clicks automatically close the dialog and return a value to the caller:

' set once (designer or code)
btnCancel.DialogResult = DialogResult.Cancel

Notes and troubleshooting:

  • Prefer Me.Close() (close current form) instead of Application.Exit() unless you truly want to shut down the entire application.
  • If Escape still does nothing, confirm KeyPreview is True, no other control or menu is swallowing the key, and the form isn’t an MDI parent (MDI behaves differently).
  • If you’re on WPF instead of WinForms, the equivalent is Button.IsCancel = True.

Use the form-level option or KeyPreview approach depending on whether you want the ESC to act like a dialog cancel (DialogResult) or to run custom cleanup logic before closing.

Recommended Answers

All 2 Replies

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
          Application.Exit()
    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
   ButtonExit.Focus()
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
   ButtonExit.Focus()
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.