是否可以避免在Winform上多次单击按钮? [英] Is it possible to avoid multiple button clicks on a 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?
注意:禁用单击处理程序的第一条语句中的按钮,然后在末尾重新启用该按钮不起作用.另外,取消订阅/订阅click事件(-=后跟+ =)也无效.
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屋!