【问题标题】:Uncheck Select All Checkbox when one checkbox is Unchecked C# in windows form当一个复选框在 Windows 窗体中未选中 C# 时,取消选中全选复选框
【发布时间】:2018-02-06 18:08:22
【问题描述】:

当你点击它时我有一个复选框,它选择所有复选框这里的问题是,当取消选中时,任何选中的复选框都没有从全选中删除我试过这个代码:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked == false) 
        checkBox9.Checked = false;
}

但是当我选中任何复选框时,它会删除所有我想取消选中的复选框,只选择所有复选框,而不是所有复选框

【问题讨论】:

  • 如果 checkBox9 是“全选”复选框 - 您的代码应该可以工作,除非您有其他事件处理程序将其设置回“已选中”或您选中“错误”复选框,这并不罕见当您为控件使用“默认”名称时

标签: c# winforms checkbox


【解决方案1】:

1 - 在您的项目中添加计时器。 2 - 在“表单加载”事件上启动计时器。 3 - 创建计时器滴答事件。

4- 写下这段代码:

checkBox1.Checked != checkBox2.Checked
checkBox2.Checked != checkBox1.Checked

【讨论】:

    【解决方案2】:

    虽然你的问题不是很清楚,但它可能解决了你的问题:

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (!CheckBox1.Checked)
            {
                if (CheckBox2.Checked)
                {
                    CheckBox2.Checked = false;
    
                }
            }
    }
    

    【讨论】:

      【解决方案3】:

      最简洁的方法是遍历所有复选框。当您选中 1 时,这将选中表单上的所有复选框。当您取消选中 1 时,也会取消选中所有复选框。

      // Register the checked changed event and point all to the same event handler
      checkBox1.CheckedChanged += CheckBox_CheckedChanged;
      checkBox2.CheckedChanged += CheckBox_CheckedChanged;
      checkBox3.CheckedChanged += CheckBox_CheckedChanged;
      
      private void CheckBox_CheckedChanged(object sender, EventArgs e)
      {
          if (sender is CheckBox == false) return;
      
          var currentCheckBox = (CheckBox) sender;
      
          foreach (var checkBox in this.Controls.OfType<CheckBox>().Where(a => a != currentCheckBox))
          {
              checkBox.Checked = currentCheckBox.Checked;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-15
        • 2021-12-23
        • 1970-01-01
        • 2020-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多