列表视图项目比较



我试过了所有我知道的方法,但还是不行。

如果listview.item(1)Text具有相同的listview.item(0)项。我可以数一下,把它改成2xitem。text吗?

的例子:listview


可口可乐可口可乐
七喜

2 x可乐1 x 7

你可以使用字典来帮助。创建string和int的字典,然后相应地更新和插入。

Dim itemDictionary As New Dictionary(Of String, Integer)
For Each item As ListViewItem In ListView1.Items
    'Loop through all item in ListView
    Dim previousValue As Integer
    If (itemDictionary.TryGetValue(item.Text, previousValue)) Then
        'If can get value add 1 to the previous value
        itemDictionary(item.Text) = previousValue + 1
    Else
        'If cant get value = not found, then add new
        itemDictionary.Add(item.Text, 1)
    End If
Next

要从字典中访问值,只需使用value和Key

For Each item In itemDictionary
    Console.WriteLine(String.Format("{0}x {1}", item.Value, item.Key))
Next

最新更新