简体   繁体   English

根据用户在Listview中单击的内容,从集合中删除对象的最佳方法是什么?

[英]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. 我正在使用C#,单击时需要从Listview中删除一个对象。 The Listview is filled by a collection and not just directly filled by the user, so I can't just use something like Listview由集合填充,而不仅仅是用户直接填充,因此我不能只使用类似

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. 我很难弄清楚如何将listView1.SelectedItems信息传递到我可以传递的正确类型的对象中,我甚至不确定这是否是正确的方法。 Thanks for any help you guys can give me. 谢谢你们给我的任何帮助。

Edit: It's a windows form 编辑:这是一个Windows窗体

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(); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从集合中删除/跳过项目的最佳方法是什么 - What is the best way to remove/skip an item from collection 从有序集合中删除项目的最佳方法是什么? - What's the best way to remove items from an ordered collection? 从数据表中删除重复项的最佳方法是什么? - What is the best way to remove duplicates from a datatable? 对对象评估用户输入的比较字符串的最佳方法是什么? - What is the best way to evaluate user inputed comparison string against a object? 从复杂对象获取有序列表的最佳方法是什么,其中列表元素来自第二代子集合 - what is the best way to get an ordered list from a complex object where the list elements are from a second generation child collection 实现property = value集合的最佳方法是什么 - What is the best way to implement a property=value collection 按季度计算日期收集的最佳方法是什么? - What is the best way to count collection of dates by quarter? 解决对象的最佳方法是什么? - What is the best way to resolve an object? 从词典中删除所有实例的最佳方法是什么? - What is the best way to remove all instances from a Dictionary? 从IEnumerable中删除项目的最佳方法是什么 <SelectListItem> ? - What is the best way to remove items from a IEnumerable<SelectListItem>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM