循环遍历checkedListBox项而不选择所有项

时间:2013-05-31 07:02:05

标签: c# checkedlistbox

我希望将checkedListBox中的项目转换为List<>,但不选择全部/取消选择全部(第一个复选框)..无法弄清楚如何不添加第一个项目.. 这是代码:

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

然后我拿走这些物品并放入另一个列表以避免错误:

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

但仍然加载了选择全部...帮助

2 个答案:

答案 0 :(得分:2)

尝试:

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

答案 1 :(得分:2)

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

之后从列表中删除第一项

list.removeat(0);
相关问题