How to disable double click on winform button?

I do not want to allow the user to double-click on my button. The first click it should be disabled, and as soon as my code is executed, I must turn on the button. How to do it?

+4
source share
6 answers

You can use the Enabled property. Register the OnClick event handler and set Enabled to false as the first. Then start the calculation and restore Enabled to true when you are done. If the calculation takes a long time, you must start the second thread. In this case, you may need to use Invoke to re-enable the button.

+8
source

Try the following:

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

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

+2
source

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

+2
source

Assuming that this is the winform information that you are talking about, after disabling the button, the event attached to it does not work. (If I remember the experience correctly)

In this case, you have two buttons, one on and one off. When the user clicks on the first button, you can hide it and show it disabled, making it look as if the same button was disabled.

Then, when you finish executing the code, try re-displaying the first button and hiding the second again.

+1
source

As far as I understand, the only goal you intend to do is to prevent the execution of the same code twice.

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

If the variable is set, the call will return another code that will be executed - ultimately, resetting the logical variable.

0
source

Source: https://habr.com/ru/post/1307629/


All Articles