禁用单击按钮事件c#
我在Visual Studio 2013 C#中编程。 我想要一个function来短时间禁用一个按钮。 我试过了button.Enabled=false
,但我看到当我点击它时它被禁用然后点击操作就会在我在程序的其他地方启用该按钮后立即启动。
如何清除该事件或在禁用按钮时阻止它们?
此致,Krzysztof
禁用按钮单击。
button1.Click -= button1_Click; // here button1_Click is your event name
启用按钮单击。
button1.Click += button1_Click; // here button1_Click is your event name
你不能在方法中执行命令之前检查按钮的状态吗? 例如:
void Click(object sender, EventArgs e) { if(!(sender as Button).Enabled) return; //other statements }
这是我在互联网上找到的非常简单的方式,它的工作原理。
- 禁用按钮
- 运行在程序上执行的任何操作
- 运行此命令
Application.DoEvent();
。 此命令将所有单击事件排入队列(与用户单击的一样多)并忽略/处置它们 - 在最后启用按钮
祝好运
尝试
YourButton.Attributes.Add("onClick", "");
完全删除它的onClick。
然后
YourButton.Attributes.Add("onClick", "YourButton_Click");
要再次添加它。
但是当你说它时你的程序不应该执行Click。 您的代码逻辑可能存在问题。
看下面的代码,单击按钮2后,按钮1被禁用。 当按钮1被禁用时,单击按钮1.按钮1在5秒后自动启用textBox1更新为“更新”。
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 = ""; }