Search code examples
c#buttonclickeventargskeyeventargs

break button_click event when enter pressed


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


Solution

  • public void btnClick(object sender, EventArgs e)
    {
      bool IsMouse = (e is System.Windows.Forms.MouseEventArgs);
    
      // If not mouse, they hit spacebar or enter
    }