【问题标题】:Loop through Listviews and their items循环遍历 Listviews 及其项目
【发布时间】:2016-12-11 14:32:12
【问题描述】:

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

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

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

【问题讨论】:

  • “这不起作用”不是对问题的准确描述。 “代码被跳过”是什么意思?
  • @Jens,当我在“For each Ctrl....”处设置断点并按 F11 时,所有代码在 If 语句之后都被跳过 - 所以它不会输入“Dim currentListview... "。
  • 如果你有 3 个列表视图,将它们放在一个数组中并循环通过 that。如果 LV 在容器中,它们将不会在 Me.Controls 中。另外,您不必投射
  • Plutonix 所说的。如果你想让它保持一般性,你必须递归地遍历所有子容器。
  • @Plutonix,是的,它们在容器中。但是如何使用数组循环项目,我无法让它工作 - 请参阅我编辑的问题。

标签: vb.net listview


【解决方案1】:

问题解决了。如果 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-07
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多