C# removing items from listbox

后端 未结 16 2252
礼貌的吻别
礼貌的吻别 2020-12-02 00:12

I have a listbox being populated from a SQLDATA pull, and it pulls down some columns that i dont want like OBJECT_dfj, OBJECT_daskd. The key is all of these being with OBJE

16条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 00:43

    The problem here is that you're changing your enumerator as you remove items from the list. This isn't valid with a 'foreach' loop. But just about any other type of loop will be OK.

    So you could try something like this:

    for(int i=0; i < listBox1.Items.Count; )
    {
        string removelistitem = "OBJECT";
        if(listBox1.Items[i].Contains(removelistitem))
             listBox1.Items.Remove(item);
        else
            ++i;
    }
    

提交回复
热议问题