[英]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.