Osvier
Osvier

Reputation: 839

WinForm button keeps focus

I have a winform with several buttons, when I hit a button, it runs the Click Event Handler, and then the button keeps selected, so if then I hit the ENTER key in the keyboard, it will run the Click event handler for that button again.

I guess this is the default behavior for a button (keeping it selected when its clicked) but I cant find a way to remove that behavior.

I tried the methods Focus() and Select() for another control, but the button is still Selected/Focused/Active

any help?

Upvotes: 1

Views: 1881

Answers (1)

sebagomez
sebagomez

Reputation: 9599

If you don´t want a user to hit enter and fire the event again while it is still running you can disable the button while running the handler code (with a finally in case something messes up)

Edit:

private void btnOk_Click(object sender, EventArgs e)
{
    btnOk.Enable = false;
    try
    {
        // do stuff
    }
    finally
    {
        btnOk.Enable = true;
    }
}

Upvotes: 2

Related Questions