簡體   English   中英

無法從列表框刪除項目

[英]Cannot remove items from ListBox

嘗試運行此方法時,出現錯誤:

此枚舉器綁定到的列表已被修改。 如果列表不變,則只能使用枚舉數。

foreach (var item in lstPhotos.SelectedItems)
{
    lstPhotos.Items.Remove(item);
}

如何刪除所選項目?

while(lstPhotos.SelectedItems.Count != 0)
{
    lstPhotos.Items.Remove(lstPhotos.SelectedItems[0]);
}

foreach循環不允許您修改要枚舉的集合。 如果您需要修改集合,請使用for循環。

編輯:

我在上面留下了我的原始答案,但是通過編碼,很明顯,由於SelectedItems屬性的動態長度,while循環更適合此問題。

while(lstPhotos.SelectedItems.Length > 0)
{
    lstPhotos.Items.Remove(lstPhotos.SelectedItems[0];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM