C# removing items from listbox

后端 未结 16 2268
礼貌的吻别
礼貌的吻别 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:40

    You want to iterate backwards through using a counter instead of foreach. If you iterate forwards you have to adjust the counter as you delete items.

    for(int i=listBox1.Items.Count - 1; i > -1; i--) {
    {
        if(listBox1.Items[i].Contains("OBJECT"))
        {
            listBox1.Items.RemoveAt(i);
        }
    }
    

提交回复
热议问题