3

Is an easy way to cancel click event when user hit enter on button (instead of mouse click on button?)

i have tried with:

    private void button3_Click(object sender, EventArgs e)
    {
        KeyEventArgs ke = e as KeyEventArgs;
        if (ke != null)
        {
            if (ke.KeyCode == Keys.Enter)
            {
                return;
            }

        }
    }

But ke is null

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
iskrzycki
  • 154
  • 2
  • 13
  • Not sure why you'd want to do this. This is a standard feature of how Windows programs work and you should strive to stay with the norm (except in some unusual situations). If you don't want it to be the default button then you can remove that from the Form's properties. – awudoin Sep 25 '13 at 18:30
  • I'm using barcode scanner in my winform. I have set up scanner to hit enter after every scan. But if button of form has focus and user scan an barcode, button will do click event (because barcode scanner scan code and 'hit' enter) – iskrzycki Sep 25 '13 at 18:37
  • I would think a better approach would be to disable the button until a valid input is entered via whatever text box. This would prevent accidental "clicking" with the scanner and would still maintain a standard that users have come to expect with WinForms. I know many users prefer to use a keyboard over a mouse (I often do) and disabling the enter button would annoy them. – awudoin Sep 25 '13 at 18:41
  • In this form user must scan few barcodes, and set few things on comboboxes, listboxes and other windows, so all of controls must be enabled during this operation – iskrzycki Sep 25 '13 at 18:44

5 Answers5

3
public void btnClick(object sender, EventArgs e)
{
  bool IsMouse = (e is System.Windows.Forms.MouseEventArgs);

  // If not mouse, they hit spacebar or enter
}
Gary Walker
  • 8,831
  • 3
  • 19
  • 41
  • BTW, I remember the way I figured out this solution originally. I ran the code in the debugger and inspected the object type when I ran it using mouse and keyboard to trigger the button and saw that they were different object types. If you use Enter/Space you get the type EventArgs, so you can't test against EventArgs as MouseEventArgs descends from EventArgs – Gary Walker Sep 25 '13 at 19:00
2

Yes it will be null. because EventArgs is not KeyEventArgs

KeyEventArgs will be passed as a parameter to KeyDown or KeyUp events. You're messing up things.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Moreover, button3_Click is event handler subscribed for a Click event for the Button class, so it meant that Button is Clicked, not Pressed – Alan Turing Sep 25 '13 at 18:30
1

You can do something like this

private bool flag = false;
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Return)
    {
        flag = true;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    if (flag)
    {
        flag = false;
        return;
    }
    //else do original task
}
Hossain Muctadir
  • 3,546
  • 1
  • 19
  • 33
0

You can handle KeyPress event for the button and disable or ignore enter key there instead of returning in button click

appcoder
  • 639
  • 1
  • 9
  • 19
0
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((keyData & Keys.KeyCode) == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
                return true;
            }
            return false;
        }
Alex Filatov
  • 2,232
  • 3
  • 32
  • 39