是否可以避免在 Winform 上多次单击按钮? [英] Is it possible to avoid multiple button clicks on a Winform?

查看:66
本文介绍了是否可以避免在 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 上多次单击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆