【问题标题】:Disable click button event c#禁用单击按钮事件c#
【发布时间】:2016-03-20 22:46:31
【问题描述】:

我在 Visual Studio 2013 C# 中编程。 我希望有一个功能可以在短时间内禁用按钮。 我试过button.Enabled=false,但我发现当我在它被禁用期间单击它时,当我在程序的其他位置启用该按钮后,单击动作就会立即开始。

当按钮被禁用时如何清除该事件或阻止它们?

最好的问候, 克日什托夫

【问题讨论】:

  • 您使用的是 WinForms 还是 WPF?
  • 正常情况下,按钮启用后不会触发该事件。因此,能否向我们展示您如何启用该按钮?

标签: c# events button


【解决方案1】:

试试

YourButton.Attributes.Add("onClick", "");

完全删除它的 onClick。

然后

 YourButton.Attributes.Add("onClick", "YourButton_Click");

再次添加。

但是当你说它执行时,你的程序不应该执行 Click。您的代码逻辑可能有问题。

【讨论】:

    【解决方案2】:

    你不能在方法中执行命令之前检查按钮的状态吗? 例如:

    void Click(object sender, EventArgs e)
    {
        if(!(sender as Button).Enabled)
            return;
    
        //other statements
    }
    

    【讨论】:

      【解决方案3】:

      禁用按钮点击。

      button1.Click -= button1_Click; // here button1_Click is your event name
      

      用于启用按钮点击。

      button1.Click += button1_Click; // here button1_Click is your event name
      

      【讨论】:

      • 您确定它适用于 C# Web 表单吗?它似乎适用于 Windows 窗体...
      【解决方案4】:

      这是我在互联网上找到的极其简单的方法。

      1. 禁用按钮
      2. 运行需要对程序执行的任何操作
      3. 运行此命令Application.DoEvent();。此命令将所有点击事件(与用户点击的数量一样多)排入队列并忽略/处置它们
      4. 最后启用按钮

      祝你好运

      【讨论】:

        【解决方案5】:

        看下面这段代码,点击button2后,button 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 = "";
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多