【问题标题】:Enabling Button after multi button Select多按钮选择后启用按钮
【发布时间】:2014-05-25 17:22:26
【问题描述】:

我正在开发一个 Windows 窗体 C# 应用程序,当且仅当所有三个按钮(按钮 1、按钮 2、按钮 3)都被单击时,我才需要在其中启用按钮 4 按钮。我正在努力实现这一点。帮我提供一些想法和示例。提前致谢。

【问题讨论】:

  • 例如,您可以为每个按钮创建一个布尔字段,并在单击按钮时将其设置为 true。
  • 哈哈。如何同时点击三个按钮?

标签: c# winforms visual-studio-2012


【解决方案1】:

我认为您正在寻找的是一个复选框。您可以使用这种代码使 CheckBoxes 看起来像按钮:

var checkBox1 = new System.Windows.Forms.CheckBox(); 
var checkBox2 = new System.Windows.Forms.CheckBox(); 
var checkBox3 = new System.Windows.Forms.CheckBox(); 
var checkBox4 = new System.Windows.Forms.CheckBox(); 
checkBox1.Appearance = System.Windows.Forms.Appearance.Button; 
checkBox2.Appearance = System.Windows.Forms.Appearance.Button; 
checkBox3.Appearance = System.Windows.Forms.Appearance.Button; 
checkBox4.Appearance = System.Windows.Forms.Appearance.Button; 

然后做你想做的事:

if (checkBox1.Checked && checkBox2.Checked && checkBox3.Checked)
    checkBox4.Enabled = true;
else
    checkBox4.Enabled = false;

或者你可以一行:

checkBox4.Enabled = checkBox1.Checked && checkBox2.Checked && checkBox3.Checked;

【讨论】:

    【解决方案2】:

    点击 3 个按钮是不可能的,因为您不能用鼠标同时按下所有三个按钮。

    如果你的意思是他们被点击了,看下面的代码。

    int index = 0;
    
    button1.Click(object sender, eventargs e)
    {
        index = index + 1;
        button1.enabled = false;
    }
    button2.Click(object sender, eventargs e)
    {
        index = index + 1;
        button2.enabled = false;
    }
    button3.Click(object sender, eventarfs e)
    {
        index = index + 1;
        button3.enabled = false;
    }
    //And now the button to see if all buttons have been clicked:
    button4.Click(object sender, eventargs e)
    {
        if (index == 3)
        {
            Messagebox.Show("All buttons have been clicked.")
        }
        else
        {
            Messagebox.Show("Not all buttons have been clicked")
        }
    }
    

    【讨论】:

      【解决方案3】:
      Dictionary<string, bool> buttonClicked = new Dictionary<string, bool>();
      private void button123_Clicked(object sender, EventArgs e)
      {
          string buttonName = (sender as Button).Name;
          if (!buttonClicked.ContainsKey(buttonName))
          {
              buttonClicked.Add(buttonName, true);
          }
          if (buttonClicked.Count == 3) button4.Enabled = true;
      }
      

      将此添加为前 3 个按钮的 OnClick 事件,它应该可以解决问题。

      【讨论】:

        【解决方案4】:

        将此列表添加到您班级的根目录:

        List<Button> pressedButtons = new List<Button>();
        

        然后将此方法添加到您的 1、2 和 3 按钮的“单击”事件中(对所有 3 个使用相同的方法):

        private void button_Click(object sender, EventArgs e)
        {
           // Add the clicked button to the list
           pressedButtons.Add((Button)sender);
        
           // If all 3 are on the list, enable button4
           if (pressedButtons.Count == 3) { button4.Enabled = true; }
        }
        

        您应该检查“CheckBox”-元素,因为它可以与 Button-appeareance 一起使用(通过设置“Appeareance”-属性。这样你可以有一个具有开关功能的按钮。

        【讨论】:

          【解决方案5】:

          您可以将按钮放在 List 或数组中,并使用 Button 的 Tag 属性。所有 3 个按钮都有一个 click 事件处理程序代码:

          //Create a list
                  List<Button> buttons = new List<Button>(){button1,button2,button3};
          

          3 个按钮的单击事件处理程序

          private void buttons_Click(object sender, EventArgs e)
              {
                  //Update Tag value
                  if (((Button)sender).Tag == null || (bool)((Button)sender).Tag == false)
                      ((Button)sender).Tag = true;
          
                  //Check state of all buttons
                  foreach (Button btn in buttons)
                  {   
                      //One of the buttons wasn't clicked
                      if ((bool)btn.Tag == false)if (((Button)sender).Tag == null || (bool)((Button)sender).Tag == false)
                          return;
                  }
                  //All buttons were clicked
                  button4.Enabled = true;
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-02
            • 1970-01-01
            • 2012-12-06
            • 2016-05-31
            • 2022-08-14
            • 2020-02-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多