【问题标题】:How to get the owner control of a ListViewItemCollection?如何获得 ListViewItemCollection 的所有者控制权?
【发布时间】:2014-02-18 20:59:19
【问题描述】:
在C# 或VB.NET 中,我想检索拥有ListViewItemCollection 的Listview 控件,我将其传递给函数,以便能够执行以下操作:
Private Sub MySub(ByVal Items As ListView.ListViewItemCollection)
' Suspend the layout on the Listview Control that owns the Items collection.
Items.SourceControl.SuspendLayout()
Do something with the Items collection...
' Resume the layout on the Listview Control that owns the Items collection.
Items.SourceControl.ResumeLayout()
End Sub
【问题讨论】:
标签:
c#
.net
vb.net
winforms
listview
【解决方案1】:
如果您要尝试创建新的 LVI 集合,则必须指定拥有它的 LV 控件:
Dim lviCol As new ListViewItemCollection(myListView)
但是即使作为只读属性,集合也不会公开所有者。相反,在将 LVI 添加到 LV (myListView.Items.Add(newLVI)) 后,所有者将通过 ListViewitem.ListView 属性变为可用:
Dim theParent As ListView = Items(0).ListView
它不会显示在对象浏览器中的 Items 集合下,而是显示在 LVI 下。当然,如果 LVI Coll 没有可爆炸的物品!
还要注意,ListViewItem.ListView 属性可以是 Nothing。创建 LVI 时,除非添加它,否则它什么都不是。同样,如果您引用了 LVI 并将其从集合中删除,则它什么也没有:
Dim lvi As ListViewItem = myLV.Items(n)
myLV.Items.RemoveAt(n)
' lvi.ListView is now Nothing, and .Index is -1, so if you need those prop
' values, save them before you remove.