C# removing items from listbox

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

    The error you are getting means that

    foreach (string item in listBox1.Items)
    

    should be replaced with

    for(int i = 0; i < listBox1.Items.Count; i++) {
        string item = (string)listBox1.Items[i];
    

    In other words, don't use a foreach.

    EDIT: Added cast to string in code above

    EDIT2: Since you are using RemoveAt(), remember that your index for the next iteration (variable i in the example above) should not increment (since you just deleted it).

提交回复
热议问题