5

I have a basic custom dialog box for use with various controls in a WinForms application. The dialog looks like:

The custom dialog

For various reasons I do not want the user to have the ability to use the Enter key to select the 'Yes' option (buttonYes). Previously I did want this behaviour and I set the AcceptButton property of the Yes button (buttonYes) accordingly. I have scince removed this, setting buttonYes's AcceptButton property to 'None', but the form still fires the buttonYes.Click event when the Enter key is pressed. I have also tried to handle the KeyPress or KeyDown events but these are not being fired when the Enter key is used. This is basic and annoying, has anyone come across this and what can I do to implement the functionality I want?

8 Answers 8

13

All the answers here are wrong or have the wrong emphasis.

enter image description here

In order to understand what is going on you have to understand how Form.ShowDialog(...) is implemented, basically. Under the hood, ShowDialog is going into its own message loop. That message loop has hard-coded logic that treats the Enter key specially.

The dialog isn't closing because the OK button has focus, an enter press happens, and WinForms executes the OK button's click logic, which closes the window. The dialog is closing because the Enter press message is being plucked out of a message loop that is running on the top of the call stack, consumed, and causing ShowDialog to break out of its loop and close the window ... so you can't get the functionality you want by handling the key press events on the button; The key press event for Enter is never dispatched. The events going through KeyPreview are just another way of getting at the same events, and so this also doesn't help.

The only thing that works is to set the form's AcceptButton property to (none). The OK button can still have its DialogResult property set to OK so doing this will not break the dialog, but with AcceptButton set to none, WinForms just ignores Enter clicks from within ShowDialog.

3
  • THIS IS THE CORRECT ANSWER!! (jwezorek, i added an image to your answer, to make it easer for people to see & digest) hopefully, it'll get up-voted beyond the "accepted", wrong answer eventually, :-)
    – mike
    Jan 1, 2019 at 1:19
  • Even my answer is upvoted higher, I believe this is the correct method to be used.
    – Marshal
    May 8, 2019 at 20:04
  • Correct answer, on a side note, if your form that have a ElementHost with a WPF control inside, that control need to have it's own button "IsDefault" = false, else you get the same behavior.
    – Zyo
    Jun 17, 2021 at 13:28
8

To fix this, set the TabStop property to False and use the code below:

private void form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter)
        e.Handled = true;
}

Or, if as you say, you are not able to break at KeyPressEvent you need to handle ProcessKeyPreview

protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m)
{
    int _ENTER = 13;

    if (m.Msg == _ENTER)
    {
        //Do nothing
    }
    return base.ProcessKeyPreview(ref m);
}
9
  • Thanks for the answer. One thing: the delegate for the KeyPress event does not contain a definition for the argument KeyEventArg. Instead it would be KeyPressEventArg and the code would be if (e.KeyChar == (char)Keys.Enter) e.Handled = true;. Thanks again for your time.
    – MoonKnight
    Feb 7, 2012 at 12:43
  • Neither of these approaches have worked (I had tried the first previously). I had hopes for the second but this is not being fired either. I am confused by this...
    – MoonKnight
    Feb 7, 2012 at 12:48
  • @Killercam: Thanks for the corrections. Is your forms KeyPreview property set to true, if not pls set it.
    – Marshal
    Feb 7, 2012 at 12:56
  • I have tried that too. The overridden event above does not fire.
    – MoonKnight
    Feb 7, 2012 at 12:58
  • @Killercam: If it is placed in codebehind of dialogbox cs file, it must fire. Make sure you haven't placed it in the file which calls the dialogbox.
    – Marshal
    Feb 7, 2012 at 13:04
4

Also, if you want to ensure users will have to click on the buttons, instead of using the keyboard, you could set the TabStop property to False on the buttons.

6
  • This has nothing to do with the question.
    – default
    Feb 7, 2012 at 11:49
  • Why not? He doesn't want the user to use the Enter key to select a button. If he set the TabStop on the buttons to false, he doesn't even need to care about the pressed keys. Feb 7, 2012 at 11:57
  • Doesn't Tabstop only affect TabStop? What does Tabstop have to do with Enter?
    – default
    Feb 7, 2012 at 12:02
  • 1
    Enter key is just submitting the button because it has the focus. If the buttons never get the focus they cannot be triggered by the keyboard. Feb 7, 2012 at 12:46
  • 1
    The event will be there, you just don't have to code anything to handle it as it won't press the buttons, cause they don't have the focus. Feb 7, 2012 at 15:14
1

Since you've already changed the Accpet button property: what about form's KeyPreview property? Is it set to false or true? Maybe you set it to true somewhere along the way and forgot about it. Set it to false.

0
1

I just had to deal with this and none of the solutions offered worked so I handle Click event like:

private void yes_Click( object sender, EventArgs e )
{
    if( e.Equals( EventArgs.Empty ) )
        return;  // ignore "Enter" key press

    // process mouse click...
}
0

You should take a look at the *.resx or *.ressources file. Search for the AcceptButton property. It may have been set in one of these files.

2
  • why should he take a look at them?
    – default
    Feb 7, 2012 at 11:38
  • Some properties are set in these files. He should clarify there haven't been set any of this properties.
    – gimbar
    Feb 7, 2012 at 11:56
0

Simple Solution I tried many of the above solutions. This is what worked for me.

In InitializeComponent, ensure that the AcceptButton is not set. Delete, or comment out, this code:
// this.AcceptButton = this.btnClose;\

And on the button, add:
this.btnClose.TabStop = false; (somebody mentioned this one above)

If that doesn't work, you can also try moving the focus off of the button, and to the window frame. this.Focus()

0

I try any methods to do this, but work for me one:

  1. Delete AcceptButton.

  2. Make a event for click on your button.

  3. Write in event following code:

    this.DialogResult = DialogResult.OK;

Your Answer

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

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