[英]Listbox removing Item
我正在嘗試使用類似listId.Items.Remove(listId.SelectedItem[x]);
在處理完每個項目之后。 顯然,這沒有任何建議:
int listCellCounter = 0;
for (int x = 0; x < listId.Items.Count; x++)
{
Console.WriteLine(txtWebUpdate.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;
}
}
如果處理順序不重要,則從最后一項開始。 例如:
for (int x = listId.Items.Count-1; x > 0; x--) {}
通常,如果您要在處理項目時將其刪除,我建議您像這樣處理列表中的選定項目:
for(int x = listId.SelectedIndex; x >= 0; x = listId.SelectedIndex)
{
// Do your processing here
listId.Items.RemoveAt(x);
listId.Update();
}
但是看起來您正在處理列表中的每個項目。 如果是這樣,也許這是適當的:
while (listId.Items.Count > 0)
{
listId.SelectedIndex = 0;
listId.Update();
// Do your processing here
listId.Items.RemoveAt(0);
}
另一個選擇是標記和清除算法。 構建要在for循環中刪除的對象列表(而不是刪除它們),然后在計算出需要刪除的對象之后刪除該列表中的所有項目。
如果您不想向用戶顯示每個項目的刪除,只需在循環完成后清空ListBox
即可:
} //end loop
listId.Clear();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.