C# 如何遍历列表框中的项目,然后删除这些项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/380451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:17:58  来源:igfitidea点击:

How do I loop through items in a list box and then remove those item?

c#listbox

提问by

I'm getting the error below when trying to loop through a listbox and then remove the item.

尝试遍历列表框然后删除项目时出现以下错误。

List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

此枚举器绑定到的列表已被修改。仅当列表未更改时才能使用枚举器。

foreach (string s in listBox1.Items)
{
    MessageBox.Show(s);
    //do stuff with (s);
    listBox1.Items.Remove(s);
}

How can I remove the item and still loop through the contents?

如何删除项目并仍然循环浏览内容?

回答by Marc Gravell

Do you want to remove all items? If so, do the foreachfirst, then just use Items.Clear()to remove all of them afterwards.

您要删除所有项目吗?如果是这样,foreach请先执行,然后再使用Items.Clear()将其全部删除。

Otherwise, perhaps loop backwards by indexer:

否则,也许由索引器向后循环:

listBox1.BeginUpdate();
try {
  for(int i = listBox1.Items.Count - 1; i >= 0 ; i--) {
    // do with listBox1.Items[i]

    listBox1.Items.RemoveAt(i);
  }
} finally {
  listBox1.EndUpdate();
}

回答by Marc Gravell

You have to go through the collection from the last item to the first. this code is in vb

您必须从最后一个项目到第一个项目遍历集合。这段代码是在 vb

for i as integer= list.items.count-1 to 0 step -1
....
list.items.removeat(i)
next

回答by Jon Limjap

Jefferson is right, you have to do it backwards.

杰斐逊是对的,你必须倒退。

Here's the c# equivalent:

这是 C# 等效项:

for (var i == list.Items.Count - 1; i >= 0; i--)
{
    list.Items.RemoveAt(i);
}

回答by Jon Skeet

Everyone else has posted "going backwards" answer, so I'll give the alternative: create a list of items you want to remove, then remove them at the end:

其他人都发布了“倒退”的答案,所以我会给出另一种选择:创建一个要删除的项目列表,然后在最后删除它们:

List<string> removals = new List<string>();
foreach (string s in listBox1.Items)
{
    MessageBox.Show(s);
    //do stuff with (s);
    removals.Add(s);
}

foreach (string s in removals)
{
    listBox1.Items.Remove(s);
}

Sometimes the "work backwards" method is better, sometimes the above is better - particularly if you're dealing with a type which has a RemoveAll(collection)method. Worth knowing both though.

有时“向后工作”方法更好,有时上述方法更好 - 特别是如果您正在处理具有方法的类型RemoveAll(collection)。值得了解两者。

回答by nruessmann

Here my solution without going backward and without a temporary list

这里我的解决方案没有倒退也没有临时列表

while (listBox1.Items.Count > 0)
{
  string s = listBox1.Items[0] as string;
  // do something with s
  listBox1.Items.RemoveAt(0);
}

回答by Matthew Wright

How about:

怎么样:

foreach(var s in listBox1.Items.ToArray())
{
    MessageBox.Show(s);
    //do stuff with (s);
    listBox1.Items.Remove(s);
}

The ToArray makes a copy of the list, so you don't need to worry about it changing the list while you are processing it.

ToArray 制作列表的副本,因此您无需担心在处理列表时它会更改列表。

回答by Somnath Kadam

You can't make modification to the collection being iterated within the ForEach block.

您不能对 ForEach 块内迭代的集合进行修改。

A quick fix is to iterate over a copy of the collection. An easy way to make this copy is through the ArrayList constructor. The DataRowView objects in the copied collection will refer to, and be able to modify, the same underlying data as your code.

快速修复是迭代集合的副本。制作此副本的一种简单方法是通过 ArrayList 构造函数。复制的集合中的 DataRowView 对象将引用并能够修改与您的代码相同的基础数据。

For Each item As DataRowView In New System.Collections.ArrayList(lbOrdersNeedToBeVoided.Items)

please read http://social.msdn.microsoft.com/Forums/en-AU/vbgeneral/thread/b4d1f649-d78a-4e5b-8ad8-1940e3379bed

请阅读http://social.msdn.microsoft.com/Forums/en-AU/vbgeneral/thread/b4d1f649-d78a-4e5b-8ad8-1940e3379bed

回答by ThunderGr

while(listbox.Items.Remove(s)) ;should work, as well. However, I think the backwards solution is the fastest.

while(listbox.Items.Remove(s)) ;也应该工作。但是,我认为向后解决方案是最快的