3

I program in Visual Studio 2013 C#. I want to have a functionality to disable for short time a button. I tried button.Enabled=false, but I see that when I click it during it is disabled then the click action starts right after I get that button enabled in other place of the program.

How to clear that event or block them when button is disabled?

Best regards, Krzysztof

Dmitry Bychenko
  • 165,109
  • 17
  • 150
  • 199
Krzysztof Bieda
  • 197
  • 1
  • 2
  • 9

5 Answers5

8

for disable button click.

button1.Click -= button1_Click; // here button1_Click is your event name

for enable button click.

button1.Click += button1_Click; // here button1_Click is your event name
Darshan Faldu
  • 1,379
  • 2
  • 14
  • 28
6

This is the extremely simple way that I found on the internet and it works.

  1. Disable the button(s)
  2. Run whatever is needed to be performed on the procedure
  3. Run this command Application.DoEvent();. This command enqueues all the click events (as many as the user clicked) and ignore/dispose them
  4. Enable the button(s) at the end

Good Luck

vich
  • 11,726
  • 13
  • 52
  • 62
user8115838
  • 61
  • 1
  • 2
3

Can't you just check for button's state before executing commands in the method? For example:

void Click(object sender, EventArgs e)
{
    if(!(sender as Button).Enabled)
        return;

    //other statements
}
0

Try

YourButton.Attributes.Add("onClick", "");

To remove its onClick altogether.

and then

 YourButton.Attributes.Add("onClick", "YourButton_Click");

To add it again.

But your program shouldn't execute the Click when you say it does. It's likely there's something wrong in your code's logic.

Иво Недев
  • 1,449
  • 1
  • 16
  • 32
0

Look at this code below, after click button2, button 1 is disabled. while button 1 is disabled, click button 1. After button 1 enable automatically after 5 seconds textBox1 update to "Update".

    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = "Update";
    }


    private void button2_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;

        Thread.Sleep(5000);

        button1.Enabled = true;

    }

    private void button3_Click(object sender, EventArgs e)
    {
        textBox1.Text = "";
    }
Gary
  • 23
  • 4