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 ?
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.
Try this one:
button.Enabled = false;
....... do main processing here ............
Thread.Sleep(1000);
Application.DoEvents();
button.Enabled = true;
In the OnClick
event handler make the first statement a button.Enabled = false;
and the last statement an button.Enabled = true;
.
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.
make myButton.Enabled = false in your mouseClick event and at the end of your code make it true
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.
精彩评论