Escape button to close Windows Forms form in C#

C#Winforms

C# Problem Overview


I have tried the following:

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if ((Keys) e.KeyValue == Keys.Escape)
        this.Close();
}

But it doesn't work.

Then I tried this:

protected override void OnKeyDown(KeyEventArgs e)
{
    base.OnKeyDown(e);
    if (e.KeyCode == Keys.Escape)
        this.Close();
}

And still nothing's working.

The KeyPreview on my Windows Forms form properties is set to true... What am I doing wrong?

C# Solutions


Solution 1 - C#

This will always work, regardless of proper event handler assignment, KeyPreview, CancelButton, etc:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    if (keyData == Keys.Escape) {
        this.Close();
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

Solution 2 - C#

You should just be able to set the Form's CancelButton property to your Cancel button and then you won't need any code.

Solution 3 - C#

Assuming that you have a "Cancel" button, setting the form's CancelButton property (either in the designer or in code) should take care of this automatically. Just place the code to close in the Click event of the button.

Solution 4 - C#

The accepted answer indeed is correct, and I've used that approach several times. Suddenly, it would not work anymore, so I found it strange. Mostly because my breakpoint would not be hit for ESC key, but it would hit for other keys.

After debugging I found out that one of the controls from my form was overriding ProcessCmdKey method, with this code:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // ...
    if (keyData == (Keys.Escape))
    {
        Close();
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

... and this was preventing my form from getting the ESC key (notice the return true). So make sure that no child controls are taking over your input.

Solution 5 - C#

You set KeyPreview to true in your form options and then you add the Keypress event to it. In your keypress event you type the following:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 27)
    {
        Close();
    }
}

key.Char == 27 is the value of escape in ASCII code.

Solution 6 - C#

You need add this to event "KeyUp".

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.Escape)
        {
            this.Close();
        }
    }

Solution 7 - C#

You can also Trigger some other form.

E.g. trigger a Cancel-Button if you edit the Form CancelButton property and set the button Cancel.

In the code you treath the Cancel Button as follows to close the form:

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Abort;
    }

Solution 8 - C#

By Escape button do you mean the Escape key? Judging by your code I think that's what you want. You could also try Application.Exit(), but Close should work. Do you have a worker thread? If a non-background thread is running this could keep the application open.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyeahumokView Question on Stackoverflow
Solution 1 - C#Hans PassantView Answer on Stackoverflow
Solution 2 - C#Shawn StewardView Answer on Stackoverflow
Solution 3 - C#Adam RobinsonView Answer on Stackoverflow
Solution 4 - C#JoelView Answer on Stackoverflow
Solution 5 - C#KristianView Answer on Stackoverflow
Solution 6 - C#Dawid BobylaView Answer on Stackoverflow
Solution 7 - C#webMacView Answer on Stackoverflow
Solution 8 - C#SwDevMan81View Answer on Stackoverflow