This solution goes through each node and check if the current node in the 'for loop' has a matching name with the previous node just added to the tree. This only works when you are sure the nodes will be sorted. I'm using "Order By" to make sure of that.
Friend Sub HandleClassClicked(node As TreeNode)
Dim classnode As TreeNode = node
Dim [class] = DirectCast(node.Tag, System.Type)
Dim count As Integer = 0
Dim previousNode As TreeNode
'only returns public methods that are always sorted
Dim publicmethods = From linqmethod In AssemblyHelper.GetMethods([class]) Where linqmethod.IsPublic Order By linqmethod.Name
If classnode.Nodes.Count = 0 Then
For Each method In publicmethods
If node.Nodes.Count > 1 AndAlso method.Name = node.Nodes.Item(count - 1).Name Then
'this line only check previous node after 1 has been added to the tree and only creates new children if the method.Name matches the previous node (node.Nodes.Item(count - 1).Name
previousNode = node.Nodes.Item(count - 1) 'Selects previous node
previousNode.Nodes.Add(BuildNode(method.Name, method)) 'Adds duplicate to previous node.
Else 'this will be a unique name case.
classnode.Nodes.Add(BuildNode(method.Name, method))
count += 1 'only increment count on unique cases or null reference will occur
End If
Next
classnode.Expand() 'the tree will show expanded and sorted.
End If
End Sub
*Basic Node Builder
Private Function BuildNode(name As String, method As MethodInfo) As TreeNode
Dim TreeNode As New TreeNode
TreeNode.Name = name
TreeNode.Text = name
TreeNode.Tag = method
Return TreeNode
End Function