2012-12-27 51 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;  
} 
相關問題