Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wnat to close my form on escape button pressed event..

how to achieve this?

any help would be appreciated.

Thank you in advance.
Posted

On key_up event of your form write this code:
C#
private void frmFeeReceipts_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

And also, check following articles on CP for some alternatives:
Close Application at Press of Escape Button[^]
Close Application at Press of Escape Button[^]
 
Share this answer
 
v2
Comments
VJ Reddy 30-Apr-12 4:24am    
Good answer. 5!
Prasad_Kulkarni 30-Apr-12 4:33am    
Thank You VJ :)
sangel1989 30-Apr-12 6:06am    
thank you
Prasad_Kulkarni 23-May-12 9:35am    
You're welcome!
1. Handle the KeyUp event for the form and put following code in it:


C#
private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
 
Share this answer
 
Comments
VJ Reddy 30-Apr-12 4:24am    
Good answer. 5!
sangel1989 30-Apr-12 5:46am    
it is same as first one
Rahul Rajat Singh 30-Apr-12 5:48am    
yes but coincidentally both of these appeared on same time i.e. we both were oblivious of other doing the same exercise.
sangel1989 30-Apr-12 6:06am    
okay, this answer works for me
thank you
The Solutin 1 by Prasad_Kulkarni and Solution 2 by Rahul Rajat Singh are good.

I want to add that when a key is pressed it will go to the control which has focus on the form. Let us say the cursor is in a TextBox. Now, if the Escape key is pressed it will not go to the Form's KeyUp event and instead it is captured by the TextBox and the KeyUp event of TextBox fires.

So, to enable the KeyUp event of Form even when the focus is on any other control, then the KeyPreview property of the Form has to be set to true as explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx[^]
Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
and under remarks as
To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event handler to true.

After setting KeyPreview property the Form's KeyUp event will fire first, then the KeyUp event of the control which has focus will be fired.
 
Share this answer
 
v3
Comments
Rahul Rajat Singh 30-Apr-12 4:37am    
This is really really useful information.
VJ Reddy 30-Apr-12 4:52am    
Thank you very much, Rahul.
Prasad_Kulkarni 30-Apr-12 4:47am    
More detailed, surely a 5!
VJ Reddy 30-Apr-12 4:52am    
Thank you, Prasad.

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