循环浏览Listviews及其项目

时间:2016-12-11 14:32:12

标签: vb.net listview

我有3个Listviews,我正在更改其中特定项目的背景颜色。现在我想在所有Listviews中更改所有项目背景颜色。我怎么能这样做?

这不起作用(代码被跳过):

For Each Ctrl As Control In Me.Controls
 If (TypeOf Ctrl Is System.Windows.Forms.ListView) Then
     Dim currentListView As System.Windows.Forms.ListView = DirectCast(Ctrl, System.Windows.Forms.ListView)
        For Each item In currentListView.Items
                 item.backcolor = SystemColors.Window
        Next
 End If
Next

编辑:

Dim test = New System.Windows.Forms.ListView() {ListView1, ListView2, ListView3}
For Each item In test
    item.BackColor = SystemColors.Window
Next

1 个答案:

答案 0 :(得分:0)

问题解决了。如果Listviews在容器中,您需要将它们放在数组中并按如下方式循环:

Dim LVies = New System.Windows.Forms.ListView() {ListView1, ListView2, Listview3}

    For Each LV In LVies
        For Each item As ListViewItem In LV.Items
                 item.BackColor = SystemColors.Window
        Next
    Next