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
  • 180,369
  • 20
  • 160
  • 215
Krzysztof Bieda
  • 187
  • 1
  • 2
  • 10

7 Answers7

10

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,471
  • 2
  • 15
  • 32
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,836
  • 13
  • 49
  • 66
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,570
  • 1
  • 20
  • 33
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
  • 51
  • 4
0

btn.Visibility = Visibility.Collapsed;

You can try this.

-1

ALL WRONG!

  1. "button1_Click" is not the standard handler behind a button, so it cannot be removed. That is your own function.
  2. YourButton.Attributes does not exist at all.
  3. Nobody even mentioned "Web Forms".
  4. Your description does not match the Windows Forms Button behavior.

The solution for the question you really asked is: Use a usual Windows Forms Button or override the click event to check again if the button is enabled.

  • 1
    This answer mostly consists of feedback on other answers. Please restrict feedback to comments, once you have the ability. In the meanwhile, [edit] your answer to focus on your proposed solution. – Jeremy Caney Oct 16 '22 at 00:24
  • You should comment to reply to other solutions. Anwser should focus on the question, regardless of other proposal – Portevent Oct 16 '22 at 08:09
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32926122) – Hoppeduppeanut Oct 19 '22 at 04:32