If you follow a pattern similar to what I've done below, you will get the desired result. And yes, you should be doing this before you add the nodes. Not adding the nodes and then rearranging them.
Output:Code:Public Class Form1 '//fields Private md5Dictionary As New Dictionary(Of String, TreeNode)() '//event handlers Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click '//lets say these are some of the hashes (two duplicates) Dim hashes = New String() {"[E886B7DEA0BE6AA0CB34F623924A3AD2]", _ "[AS823N239XM29323RKJ2H3K23KD2K3SS]", _ "[KJH23KH2K3T239829829230H23KK3H2K]", _ "[E886B7DEA0BE6AA0CB34F623924A3AD2]"} '//lets say that these 4 files correspond to the above 4 hashes Dim files = New String() {"C:\test\file.txt", _ "C:\Program Files\system.dll", _ "C:\test\source.xml", _ "C:\test\file - Copy.txt"} Dim fileIndex = 0 For Each hash In hashes '//check in your dictionary if the hash has already been added If Me.md5Dictionary.ContainsKey(hash) Then '//we've already seen this hash Me.md5Dictionary(hash).Nodes.Add(files(fileIndex)) Else '//haven't encountered this hash yet Dim node = Me.TreeView1.Nodes.Add(hash) '//add the actual file value to the hash node node.Nodes.Add(files(fileIndex)) '//add a new dictionary entry Me.md5Dictionary.Add(hash, node) End If fileIndex += 1 Next Me.TreeView1.ExpandAll() End Sub End Class
![]()




Reply With Quote