Click here to Skip to main content
15,746,302 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi All,

I m using window application in VS2008.

How to use Esc key for closing form using
any code or idea that will be appreciate......


1 solution

2 Options:

1. In your Forms designer or in your code, set the Form.CancelButton property to your "Cancel" button. Then, when the Cancel button is pressed, or the Escape key is clicked (or even if the Dialog is closed) then the DialogResult of the form will be "DialogResult.Cancel."

2. Use keyDown event and trap KeyCode:
Private Sub Form2_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

Share this answer
[no name] 11-Jun-11 2:55am    
good answer my 5!
Silver Del 22-Sep-14 19:49pm    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900