C#: How to make pressing enter in a text box trigger a button, yet still allow shortcuts such as "Ctrl+A" to get through?

Jonathan Chan picture Jonathan Chan · Feb 6, 2010 · Viewed 111.1k times · Source

Sorry for the long title, but I couldn't think of another way to put it.

I have this:

    private void textBoxToSubmit_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            buttonSubmit_Click((object)sender, (EventArgs)e);
        }
    }

... in order to make pressing enter in the text box trigger the "submit" button. However, this also prevents shortcuts from going through. (not quite sure what it has to do with that, maybe only multi-key combos?)

ShortcutsEnabled is set to true.

Thanks in advance!

Answer

Rob Cooper picture Rob Cooper · Feb 6, 2010

Can you not use AcceptButton in for the Forms Properties Window? This sets the default behaviour for the Enter key press, but you are still able to use other shortcuts.