Click here to Skip to main content
15,748,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have developing an Windows Form application in C# Visual Studio 2012.I want to close the application by pressing "ESC" key.My doubt is which keypress event should I use? Form_KeyPress or textBox_KeyPress?
Posted
Updated 6-Dec-20 5:24am

If you set the Form's 'KeyPreview Property to 'true, then it will handle a Key Event before any of the Form's Controls get the Key Event.

I would strongly suggest ... since you are doing something which is not quite standard Win UI design ... that you build in a safety mechanism so the user doesn't cancel the Application ... possibly losing data:
C#
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        if 
        (
            MessageBox.Show
            (
                "Quit the Application",
                "Exit Application Dialog",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2 // hit Enter == No !
            )
            == DialogResult.Yes
        )
        {
           Application.Exit();
        }
    }
}
I prefer to use the KeyUp Event. In "real-world code" I'd would be implementing some level of warning related to whether or not the user had saved their work !
 
Share this answer
 
Comments
Ron Beyer 5-Dec-13 8:14am    
+5 for the confirmation
Karthik_Mahalingam 5-Dec-13 10:18am    
good..
I suppose tou should use the Form ProcessKeyPreview[^].
 
Share this answer
 
I would suggest that this is a good time to experiment and see what happens.

What if the textbox doesn't have focus - does your form exit?

This will lead you to the correct decision.
 
Share this answer
 
You can use this :-

VB
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.Esc Then
          End 
         'or
         Me.Close()
    End If
End Sub


write below line on button's close event and set the form's "CancelButton" property to cancelButton (name of the cancel button that you have).......
VB
Me.Close()



All the best...................
 
Share this answer
 
just write this on Form1_KeyDown event
if(e.KeyData==Keys.ESC)
{
    this.close();
}
 
Share this answer
 
You could over-ride the ProcessCmdKey method. Just add this code and it jsut works.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    bool fHandled = false;

    switch (keyData)
    {
        case Keys.Escape:
            fHandled = true;
            Close();
            break;
        default:
            break;
    }

    return (fHandled || base.ProcessCmdKey(ref msg, keyData));
}
 
Share this answer
 

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