views:

41

answers:

1

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?

+1  A: 

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;
    }
}
sebastian
I cant disable the button, because the users should be able to click it with the button. I also cant disable the ENTER key (it's used for another things)
Osvier
I mean disable it on the handled event... that should be the first thing you should do and then enable it backa again of course
sebastian
Thanks man!, that worked :)
Osvier