【问题标题】:Check all checkboxes in checkboxlist with one click using c#使用c#一键检查复选框列表中的所有复选框
【发布时间】:2012-12-12 15:43:47
【问题描述】:

我想要一个按钮,一旦单击,它将选中我的复选框中的所有复选框。我已经搜索了可能的答案,但我总是看到 asp.net 和 javascript 的示例。我在 c# 中使用 Windows 窗体。感谢您的回复。

【问题讨论】:

  • @Likurg,我试过了,看起来不错,但对我不起作用:for(int i = 1; i < checkedlistBox.Items.Count; i++) checkedlistBox.SetItemChecked (i, true);

标签: c# winforms button checkbox checklistbox


【解决方案1】:

在多次遇到这个问题后,我决定用扩展方法一劳永逸地为自己解决。

public static class Extensions
{
    public static void CheckAll(this CheckedListBox checkedListBox, bool check)
    {
        for (int i = 0; i < checkedListBox.Items.Count; i++)
            checkedListBox.SetItemChecked(i, check);
    }
}
MyCheckedListBox.CheckAll(true);

【讨论】:

    【解决方案2】:

    我所做的是将它放在 tableLayoutPanel 中,我修复了第 3 列中的所有复选框并添加了事件:

    private void cbCheckAllCHECKBOXs_CheckedChanged(objects sender, EventArgs e)
    {
        if (cbCheeckAllCHECKBOXs.Checked)
        {
            for (int i = 0; i < tlpCHECKBOXsControlPanel.RowCount; i++)
            {
                ((System.Windows.Forms.CheckBox)(tlpCHECKBOXsControlPanel.GetControlFromPosition(3, i))).Checked = true;
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      在 C# 后面的代码中调用一个方法并编写这段代码,然后您就可以选中/取消选中它们。这将选中或取消选中复选框列表中存在的所有复选框。希望它可能会有所帮助。

      foreach (ListItem item in CheckBoxList.Items)
      {
          item.Selected = true;    
      }
      

      【讨论】:

        【解决方案4】:

        试试这个...

            protected void chk_CheckedChanged(object sender, EventArgs e)
            {
                CheckBox[] boxes = new CheckBox[7];
                boxes[0] = this.CheckBoxID;
                boxes[1] = this.CheckBoxID;
                boxes[2] = this.CheckBoxID;
                boxes[3] = this.CheckBoxID;
                boxes[4] = this.CheckBoxID;
                boxes[5] = this.CheckBoxID;
                boxes[6] = this.CheckBoxID; //you can add checkboxes as you want
        
                CheckBox chkBox = (CheckBox)sender;
                string chkID = chkBox.ID;
                bool allChecked = true;
        
                if (chkBox.Checked == false)
                    allChecked = false;
        
                foreach (CheckBox chkBoxes in boxes)
                {
                    if (chkBox.Checked == true)
                    {
                        if (chkBoxes.Checked == false)
                            allChecked = false;
                    }
                }
                this.CheckBoxIDALL.Checked = allChecked; //Here place the main CheckBox
            }
        

        【讨论】:

          【解决方案5】:

          试试这个:

           foreach(Control c in this.Controls) {
              if (c.GetType() == typeof(CheckBox)) {
                 ((CheckBox)c).Checked = true;
              }
           }
          

          【讨论】:

            【解决方案6】:
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, true);
            }
            

            【讨论】:

            • 我之前尝试过这段代码,但没有用。现在它是。,Magic.. :) 谢谢@SekaiCode。
            猜你喜欢
            • 1970-01-01
            • 2012-01-01
            • 1970-01-01
            • 2017-05-31
            • 2014-08-09
            • 2012-12-13
            • 2016-11-30
            • 2020-03-29
            相关资源
            最近更新 更多