本文介绍了是否可以避免在 Winform 上单击多个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您在一个表单上有一个按钮,该按钮在文本框中计数为 1000,然后将其清除.

Suppose you have a button on a form that counts to 1000 in a textbox and then clears it.

如果我快速单击按钮五次(在运行时),Click 事件处理程序将被调用 5 次,我将看到计数为 1000 次.

If I quickly click the button five times (in runtime) the Click event handler will be called 5 times and I will see the count to 1000 five times.

是否可以在第一次点击计数时禁用对该按钮的其他点击?

Is it possible to disable other clicks on that button while the first click is counting?

注意:在单击处理程序的第一个语句中禁用按钮,然后在最后重新启用是行不通的.此外,取消订阅/订阅点击事件(-= 后跟 +=)也不起作用.

Note: Disabling the button in the first statement of the click handler and then re-enabling at the end does not work. Also, unsubscribing/subscribing to the click event (the -= followed by +=) does not work.

这里有一个示例来说明:

Here a sample to illustrate:

  private bool runningExclusiveProcess = false;

    private void button1_Click(object sender, EventArgs e)
    {
        this.button1.Click -= new System.EventHandler(this.button1_Click);

        if (!runningExclusiveProcess)
        {
            runningExclusiveProcess = true;
            button1.Enabled = false;


            textBox1.Clear();
            for (int i = 0; i < 1000; i++)
            {
                textBox1.AppendText(i + Environment.NewLine);
            }


                runningExclusiveProcess = false;
            button1.Enabled = true;
        }

        this.button1.Click += new System.EventHandler(this.button1_Click);
}

推荐答案

在第一次点击后禁用按钮,运行一个计时器一秒钟,它会在滴答声时重新启用按钮并自行禁用

Just disable button after initial click, run a timer for a second which will on tick reenable the button and disable itself

这篇关于是否可以避免在 Winform 上单击多个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:36