22

Often when using software these days, the ESC key will close a dialog without persisting any changes I've made. I like that especially because even though the dialog may have a cancel button on it, I don't necessarily want to reach for the mouse or tab over to the cancel button. It's a nice clean way of saying "Oops, didn't meant to do that!" to the software. In fact, I find I get annoyed with software that doesn't implement this feature and I can't believe that it's not already done under the hood of Winforms - seems intuitive to me...

I have looked at hooking into the Form KeyPress event, or trying to simulate a "Cancel" DialogResult etc. There seem to be a number of ways of going about this...

So the question is: What is the cleanest way of going about binding the ESC key to close the dialog without saving any changes?

Any help from those that have experience in this is much appreciated!

6 Answers 6

32

Set the CancelButton property of the form to reference your Cancel button.

5
  • 7
    There is also a corresponding "AcceptButton" for the enter key. The advantage of this answer method is that there's more to hooking enter and escape than keypresses - using this method will also render the correct visual hints on the buttons (for example, the AcceptButton gets a thicker border).
    – stusmith
    Commented May 15, 2009 at 13:47
  • 6
    Don't forget to also set the DialogResult property of your cancel and OK buttons.
    – NascarEd
    Commented May 15, 2009 at 14:53
  • 1
    Also, don't name your button "CancelButton". It will not work properly with the CancelButton property will silently fail.
    – bruestle2
    Commented Sep 2, 2020 at 19:00
  • 1
    @bruestle2, thank you so much for your comment! I have been trying to figure out why this wouldn't work in my code, and it is because my button was named, "CancelButton"! I never would have thought of checking that! Commented Oct 16, 2021 at 16:29
  • Also note, the cancel button needs to be Visible or it won't work. I thought I could hide the button and get the same affect .... no go
    – PandaWood
    Commented Nov 25, 2021 at 3:28
13

To do this when you don't have a cancel button you can override processcmdkey:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
  if (keyData == Keys.Escape)
  {
    this.Close();
    return true;
  }
  return base.ProcessCmdKey(ref msg, keyData);
}
1
  • 4
    You could insert a DialogResult = DialogResult.Cancel; just before your line this.Close();.
    – Uwe Keim
    Commented Jun 14, 2012 at 5:13
6

You can also set the CancelButton property of the form to the cancel button:

this.CancelButton = this.cancelButton;

In Visual Studio you can set this via the Properties of the form and the code is added to the Form.Designer.cs file

2

You can do this:

button b = new button();
b.click = button_click;
this.cancelbutton = b;


protected void button_click(object sender,eventargs e)
{
    this.close();
}
0
1

To a certain extent it depends on your development environment - in .NET its built in (as it was in vb.old), forms has a "Cancel Button" property "If this property is set, the button is 'clicked' whenever the user presses the 'ESC' key."

There is a corresponding "AcceptButton" property for the 'ENTER' key.

From memory, standard message and dialog boxes will follow the above logic by default - though of course one can specify the default button for a message box where you don't want stuff to happen without the user positively asserting that that's what they want to do.

0
    If e.KeyCode = Keys.Escape Then
        If MsgBox("Are you sure to exit system (Y/N) ?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then Exit Sub
        dashboard.Close()

    ElseIf e.KeyCode = Keys.F5 Then
        MessageBox.Show("F5")
    End If

Try this guys.This is For Show an messageBox for confirmation of Yes or No Before Closing The Forms.you can add this code in to your form inside the the form Event named as Keydown. Here "dashboard" is the name of Our Form to be closed. This Code Shows You A Message Box With Yes or No buttons. If You select Yes means It will Close your Form.If you Select No means the Action will cancel. The Form Remains Same

5
  • 1
    Please add some explanation of your answer. :) Commented Jul 11, 2017 at 11:44
  • yup Sure..you can add this code in to your form inside the the form Event named as Keydown. Here "dashboard" is the name of Our Form to be closed. This Code Shows You A Message Box With Yes or No buttons. If You select Yes means It will Close your Form.If you Select No means the Action will cancel. The Form Remains Same
    – Prakash
    Commented Jul 11, 2017 at 12:06
  • This is comment section, and this area is not for your answer.Click the edit link and add your explanation and update your answer. Commented Jul 11, 2017 at 12:15
  • Ok.I m new to this Website...Thats why little bit confusion
    – Prakash
    Commented Jul 11, 2017 at 12:19
  • Thank You So Much
    – Prakash
    Commented Jul 11, 2017 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.