簡體   English   中英

根據用戶在Listview中單擊的內容,從集合中刪除對象的最佳方法是什么?

[英]What is the best way to remove an object from a collection based upon what the user clicks in a Listview?

我正在使用C#,單擊時需要從Listview中刪除一個對象。 Listview由集合填充,而不僅僅是用戶直接填充,因此我不能只使用類似

foreach ( ListViewItem eachItem in listView1.SelectedItems)
{
    listView1.Items.Remove(eachItem);
}

因為該對象仍在集合中。 我很難弄清楚如何將listView1.SelectedItems信息傳遞到我可以傳遞的正確類型的對象中,我甚至不確定這是否是正確的方法。 謝謝你們給我的任何幫助。

編輯:這是一個Windows窗體

在這種簡單情況下,向后標准循環可以使您的生活更輕松

for(int x = listView1.SelectedItems.Count - 1; x>=0; x--)
{
    ListViewItem lvi = listView1.SelectedItems[x];
    if(YourConditionToDeleteTheItem() == true)
    {
        lvi.Remove(); 
    }
}

暫無
暫無

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

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