开发者

Listbox removing Item

I'm trying to use something like listId.Items.Remove(listId.SelectedItem[x]); after each item has been processed. Obviously that doesn't work any suggestions:

int listCellCounter = 0;

for (int x = 0; x < listId.Items.Count; x++)
{
    Console.WriteLine(txtWebUpda开发者_如何学Pythonte.Text + listId.Items[x].ToString() + "&ire=1", listCell.Items[listCellCounter].ToString());
    listId.SelectedIndex = x;
    listCell.SelectedIndex = listCellCounter;
    lblID.Text = listId.Items[x].ToString();

    if (listCellCounter == listCell.Items.Count - 1)
    {
        listCellCounter = 0;
    }
    else
    {
        listCellCounter += 1;
    }
}


If the order of processing is not critical then start at the last item. For example:

for (int x = listId.Items.Count-1; x > 0; x--) {}


Another alternative is a mark and sweep algorithm. Build a list of objects to delete in your for loop (instead of deleting them) and then delete all the items in that list after you have calculated which objects need to be deleted.


Normally I would suggest processing the selected items in a list like this if you're removing them as they are processed:

for(int x = listId.SelectedIndex; x >= 0; x = listId.SelectedIndex)
{
   // Do your processing here
   listId.Items.RemoveAt(x);
   listId.Update();
}

But it looks like you're processing every item in the list. If that's the case, maybe this is appropriate:

while (listId.Items.Count > 0)
{
   listId.SelectedIndex = 0;
   listId.Update();
   // Do your processing here
   listId.Items.RemoveAt(0);
}


If you don't want to display the removal of each item to the user, just empty the ListBox when the loop is complete:

} //end loop

listId.Clear();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜