简体   繁体   中英

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

I'm using C# and I need to remove an object from my Listview when clicked. The Listview is filled by a collection and not just directly filled by the user, so I can't just use something like

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

because the object is still in the collection. I'm having a hard time figuring out how to make pass the listView1.SelectedItems information into an object of the right type that I can pass, and I'm not even sure if that is the correct way to go about it. Thanks for any help you guys can give me.

Edit: It's a windows form

In this simple case a backward standard loop could make your life easier

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM