首页 > 解决方案 > 如何检查项目是否存在于另一个列表视图中?

问题描述

所以我有两个列表视图。我想检查 Listview1 中的项目 X 是否已存在于 ListView2 中,以及它是否改变了 ListView2 中项目的颜色。

For Each itm As ListViewItem In Form1.ListView1.Items
    If itm.SubItems(0).Text.Contains(stringJoined) Then
        itm.BackColor = Color.Red
    End If
Next

标签: vb.net

解决方案


您可以使用第二个列表视图的Items.ContainsKey方法:

For Each itm As ListViewItem In Form1.ListView1.Items

    ' Check for item is ListView2
    If Form1.ListView2.Items.ContainsKey(itm.Key) Then
        ' Set BackColor to Red
        Form1.ListView2.Items.Item(itm.Key).BackColor = Color.Red
    End If

Next

这假设您使用键填充了两个列表视图,并且这两个项目将具有相同的键。


推荐阅读