如何获取ListViewItemCollection的所有者控件?

时间:2014-02-18 20:59:19

标签: c# .net vb.net winforms listview

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

1 个答案:

答案 0 :(得分:1)

如果您尝试创建新的LVI集合,则必须指定拥有它的LV控件:

Dim lviCol As new ListViewItemCollection(myListView)

但该集合甚至不会将所有者公开为Readonly属性。相反,在将LVI添加到LV(myListView.Items.Add(newLVI))之后,所有者可通过ListViewitem.ListView属性获得:

Dim theParent As ListView = Items(0).ListView 

它不会在Items集合下的Object Browser中显示,但在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.