选中复选框列表中的所有复选框,只需单击一下c#

我希望有一个按钮,一旦点击,它将选中我的核对表框中的所有复选框。 我搜索了可能的答案,但我总是看到asp.net和javascript的例子。 我在c#中使用Windows表单。 谢谢你的回复。

 for (int i = 0; i < checkedListBox1.Items.Count; i++) { checkedListBox1.SetItemChecked(i, true); } 

试试这个…

  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 } 

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

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

试试这个:

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