How to close a window when Esc is pressed
Windows Forms
3
Posts
3
Posters
0
Views
1
Watching
-
I want to know how to close the windows form when Esc button is pressed.
Rangasamy
-
I want to know how to close the windows form when Esc button is pressed.
Rangasamy
Hi Ragu, Firsy you have to change this property on form KeyPreview = true You can change this on the form properties in designer or in the form load method:
private void Form1\_Load(object sender, EventArgs e) { this.KeyPreview = true; }
And then, you only have to do this:
private void Form1\_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) this.Close(); }
That´s it?
----- LeandroAB
-
I want to know how to close the windows form when Esc button is pressed.
Rangasamy
Set the form's
CancelButton
property to a button that will close the form."Multithreading is just one damn thing after, before, or simultaneous with another." - Andrei Alexandrescu