none
Prevent a button to react on Enter key RRS feed

  • Question

  • How to prevent a button from react on Enter key press ?

    It's important to understand that the 'Enter' keypress can't be filtered out, I still have to use the Enter 'sign'. Only the Button should not react on it

    Saturday, March 3, 2012 11:22 AM

Answers

  • In keyDown event:

    if(e.KeyCode != Keys.Enter)
    {
        //code will go in here if any key is press except Enter
    }


    Mitja

    • Proposed as answer by Ahmed Naji Saturday, March 3, 2012 12:23 PM
    • Marked as answer by Pivskid Monday, March 5, 2012 9:38 AM
    Saturday, March 3, 2012 11:49 AM

All replies

  • In keyDown event:

    if(e.KeyCode != Keys.Enter)
    {
        //code will go in here if any key is press except Enter
    }


    Mitja

    • Proposed as answer by Ahmed Naji Saturday, March 3, 2012 12:23 PM
    • Marked as answer by Pivskid Monday, March 5, 2012 9:38 AM
    Saturday, March 3, 2012 11:49 AM
  • Hi,

    If you are focused on the button and press 'enter' on the keyboard the 'Enter' keypress event doesn't raise (react) - but by pressing 'space' then it raises a buttonClick event and a keyPressEvent..

    sounds a little confused.. but when does the 'enter' keyPress on a button raises that event?

    Regards Muli


    If some code doesn't work, don't worry help is on the way.. don't forget to mark your thread as solved when done...

    Saturday, March 3, 2012 12:55 PM
  • Perhaps all that is necessary is to set the form's AcceptButton, as documented here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton.aspx.
     

    --
    Mike
    • Proposed as answer by Neddy Ren Monday, March 5, 2012 6:44 AM
    Saturday, March 3, 2012 1:03 PM
  • Maybe the PreviewKeyDown event can be used. If not, you can try deriving from Button and overriding the IsInputKey method.
    Monday, March 5, 2012 8:13 AM