2012-12-27 52 views
12

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

+0

@Likurg,我想这一点,似乎不错,但我没有工作: '的for(int i = 1; I Brenelyn

回答

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

前段时间我试过这段代码,但没有工作。现在是魔术..谢谢@SekaiCode。 – Brenelyn

+0

非常感谢。你也解决了我的问题:) –

0

试试这个:

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

尝试......

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 
    } 
2

呼叫从后面的代码在C#中的方法,写这一段代码,那么你可以能够选中/取消选中它们。这将检查或取消选中复选框列表中的所有复选框。希望它可以帮助。

foreach (ListItem item in CheckBoxList.Items) 
{ 
    item.Selected = true;  
} 
相关问题