Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从CheckedListBox中删除多个选中项_C#_Winforms_Checkedlistbox - Fatal编程技术网

C# 如何从CheckedListBox中删除多个选中项

C# 如何从CheckedListBox中删除多个选中项,c#,winforms,checkedlistbox,C#,Winforms,Checkedlistbox,我知道如何从checkedlistbox中删除单个checkedItem。但是现在,我想一次性删除所有选中的项目 我试过这个: foreach (var item in List_Frente.CheckedItems) { List_Frente.Items.Remove(item); } 但您可能知道,它给了我一个错误,即该枚举器绑定到的列表已被修改。枚举数只能在列表未更改的情况下使用。如何通过单击删除所有ch

我知道如何从
checkedlistbox
中删除单个
checkedItem
。但是现在,我想一次性删除所有选中的项目

我试过这个:

foreach (var item in List_Frente.CheckedItems)
            {
                List_Frente.Items.Remove(item);
            }

但您可能知道,它给了我一个错误,即该枚举器绑定到的
列表已被修改。枚举数只能在列表未更改的情况下使用。
如何通过单击删除所有
checkeditems

这是因为您修改了正在迭代的列表。使用for语句防止发生这种情况:

for(var i=0; i<List_Frente.CheckedItems.Count; i++)
{
    ((IList)List_Frente.CheckedItems).Remove(List_Frente.CheckedItems[0]);
}

for(var i=0;i您可以这样做:

foreach (var item in List_Frente.CheckedItems.OfType<string>().ToList())
{
    List_Frente.Items.Remove(item);
}
foreach(列表\u Frente.CheckedItems.OfType().ToList()中的变量项)
{
列出当前项目。删除(项目);
}
如果要将所有内容都写在一行中:

List_Frente.CheckedItems.OfType<string>().ToList().ForEach(List_Frente.Items.Remove);
List\u frentem.CheckedItems.OfType().ToList().ForEach(List\u Frente.Items.Remove);
当然,这只适用于相同类型的物品。不过看起来还是很粗糙

编辑说明:
ToList()
创建一个新列表,因此,可以更改原始CheckedItems列表,因为枚举器现在枚举的是我们新创建的列表,而不是原始列表。type()
之所以在那里,是因为我们需要调用
ToList()
这个方法有效

object[] items = new object[checkedListBox.Items.Count];
checkedListBox.Items.CopyTo(items, 0);
checkedListBox.Items.Clear();
checkedListBox.Items.AddRange(items);

我的目标是返回一个更漂亮、更少黑客的解决方案。

可能是这篇文章的附加组件

while (checkedListBox.CheckedItems.Count > 0) {
   checkedListBox.Items.RemoveAt(checkedListBox.CheckedIndices[0]);
}
清除所有项目

checkedListBox1.Items.Clear();

不要使用
foreach
。使用
for
。我不熟悉winforms,但是你能做一个
列表吗?CheckedItems.ToList()
?如果可以的话,这将解决问题problem@HighCore不是,只是
ToString()
=(@HighCore
foreach
使用迭代器。当存在活动迭代器时,您不能对集合进行更改。在当前形式下,您不能使用
foreach
for(i=0;i<(List_Frente.Items.Count;i++)
检查此页面:嗨!没有
chedkims.REMOVE()这样的方法)
。我试过了,但它不存在;\n哦,是的,没有ToList()还有;嘘。我仔细看了一看,其中清楚地说明了有一个
Remove
-方法-除了它需要显式实现之外。我将相应地修改我的答案。@Ghaleon我修复了代码。在.NET的早期版本中引入的这些类有时并不像新版本那样舒适;)@Spontifixus也谢谢你,伙计!我会把你的代码保存在我的codes.txt上hehe xD+1OMG成功了!!谢谢Jobo!欢迎你,如果你接受答案,这里的所有人都会有所帮助:)我知道,我只是在做一些测试,我必须等几分钟,然后马克才能给出答案;P……你能解释一下为什么这样做吗?而不是我这样做吗?@Ghaleon,因为LinQ方法
of type
ToList()
创建一个
列表的新实例,然后对其进行迭代。而不是迭代原始集合,如果在迭代过程中删除了某个项,则会产生异常。@Jobo Nice one!您可以将LINQ变量添加到您的答案中:
List\u Frente.CheckedItems.OfType().ToList().ForEach(List_Frente.Items.Remove);
+1可以确认这是有效的。我觉得自己没有看到它有点傻,也许这就是为什么这没有任何投票权的原因吧?不管怎样,谢谢。