JackIta JackIta - 9 days ago 7
C# Question

Remove possibility to trigger a button with a keyboard input

I have a standard button, with nothing interesting in it, and everything works perfectly.

private void Button_Click(object sender, EventArgs e)
{
//Whatever, is not important what's in it
}


The problem is, when i click on a button, if after that i press for example enter key or the space key, it will trigger the button as if i clicked it.

i have tried looking online, but i couldn't find an answer, i tried messing with the properties and is no help either.

How do i prevent that from happening in a (possibly) easy way?

Answer for anyone who needs it(to put inside the button):

if (e is MouseEventArgs)
{
//what you want in for the button to do
}

Answer

You can check the type of the given EventArgs, if the button is clicked e is a MouseEventArgs (and will contain info about the click), otherwise is just a EventArgs

private void button1_Click(object sender, EventArgs e)
{
    if (e is MouseEventArgs)
    {
        // Is mouse click
    }
    else
    {
        // Not mouse click...
    }
}
Comments