2013-05-31 123 views
3

我想從checkedListBox項目變爲List<>,但沒有選擇所有/全部取消選擇(第一個複選框)..不知道如何添加第一項.. 這是代碼:循環通過checkedListBox項目沒有選擇所有項目

foreach (string s in checkedListBoxDepts.CheckedItems) 
{ 
     if (checkedListBoxDepts.SelectedItems.IndexOf(s) == 0) 
      continue; 
     list.Add(s); 
} 

然後我拿物品,放在另一個列表,以避免錯誤:

foreach (string s in list) 
{ 
    list2.Add(s); 
} 

,但仍選擇所有加載...幫助

+0

顯示烏爾checkedBoxList – tariq

回答

2

嘗試:

foreach (var s in checkedListBoxDepts.CheckedItems) 
{ 
     if (checkedListBoxDepts.IndexOf(s) == 0) 
      continue; 
     list.Add(s.ToString()); 
} 
+0

的標記類型轉換爲什麼在每次迭代中是不是最好使用「字符串s」中var s在foreach中的位置? – tariq

+0

這不是鑄造,而是隱式輸入。這意味着編譯器會自己計算變量的類型。 – Nair

+0

在家裏這個代碼不工作,必須是這樣的: checkedListBoxDepts.SelectedItems.IndexOf – themadmax

2
foreach (string s in checkedListBoxDepts.CheckedItems) 
{ 
    if (checkedListBoxDepts.SelectedItems.IndexOf(s) == 0) 
     continue; 
    list.Add(s); 
} 

後,從列表中刪除第一項

list.removeat(0); 
+0

剛剛刪除了全選選項... – Paradigm

相關問題