Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop the Enter Key from Firing on a WinForm

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?

like image 629
MoonKnight Avatar asked Feb 07 '12 11:02

MoonKnight


People also ask

What are Windows form controls in C#?

Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.


3 Answers

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.

like image 96
jwezorek Avatar answered Oct 21 '22 15:10

jwezorek


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);
}
like image 28
Marshal Avatar answered Oct 21 '22 17:10

Marshal


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.

like image 4
Anderson Pimentel Avatar answered Oct 21 '22 16:10

Anderson Pimentel