Anuya
Anuya

Reputation: 8350

How to disable double click on winform button?

I don't want to allow user to double click on my button. On the first click it should be disabled and once my code is executed, i should enable the button. How to do ?

Upvotes: 5

Views: 12615

Answers (6)

Ron
Ron

Reputation: 49

Try this one:

button.Enabled = false;
....... do main processing here ............ 
Thread.Sleep(1000);
Application.DoEvents();
button.Enabled = true;

Upvotes: 4

MSIL
MSIL

Reputation: 2761

As far as I can understand, the only purpose you intend to do this is preventing the same code to be executed twice.

Why not use a class level boolean variable inside the handler method.

If the variable is set, the call will return else the code will be executed - ultimately resettng the boolean variable.

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104198

You can use the Enabled property. Register an OnClick event handler and set Enabled to false as the first thing. Then start you computation and restore Enabled to true, when you are finished. If the computation takes long to complete, you should start a second thread. In that case, you may need to use Invoke method to re-enable the button.

Upvotes: 8

Jamie Dixon
Jamie Dixon

Reputation: 54011

Assuming this is a winform you're talking about, once you disable the button, the event attached to it won't fire. (If i remember correctly from experience)

The way around this is to have 2 buttons, one enabled and one disabled. When the user clicks the first button you can then hide it and show the disabled one making it look like the same button was disabled.

Then when you've finished executing your code, simple re-show the first button and re-hide the second.

Upvotes: 1

Amsakanna
Amsakanna

Reputation: 12954

make myButton.Enabled = false in your mouseClick event and at the end of your code make it true

Upvotes: 1

Oded
Oded

Reputation: 499302

In the OnClick event handler make the first statement a button.Enabled = false; and the last statement an button.Enabled = true;.

Upvotes: 1

Related Questions