سؤال

I have already filtered out Tree-nodes into a list that contain the same Node Name with "IsDuplicateNodeName"[1].

I want to add the nodes from that lists into a child node with the same Children Name being expandable showing all the children with the same name with different internal Values, this is where I'm having trouble.[2]

I'm looking for something like this from the "nodeList" I already have.
NameDuplicate1
-NameDuplicate1
-NameDuplicate1
NameDuplicate2
-NameDuplicate2
-NameDuplicate2
-NameDuplicate2

[1]

Friend Sub HandleClassClicked(node As TreeNode)

        Dim classnode As TreeNode = node
        Dim [class] = DirectCast(node.Tag, System.Type)
        Dim nodeList As New List(Of TreeNode)

        'only returns public methods
        Dim publicmethods = From linqmethod In AssemblyHelper.GetMethods([class]) Where linqmethod.IsPublic

        If classnode.Nodes.Count = 0 Then
            For Each method In publicmethods
                If IsDuplicateNodeName(method.Name, method, node) Then 'Storing Duplicate Nodes
                    nodeList.Add(classnode)
                Else
                    classnode.Nodes.Add(BuildNode(method.Name, method)) 'If Unique adding to Tree
                End If
            Next
            AddDuplicateNodes(nodeList) 'This method handles the duplicate List of Nodes.
            classnode.Expand()
        End If
    End Sub

[2]

Friend Sub AddDuplicateNodes(nodeList As List(Of TreeNode))

        For Each node In nodeList 'Iterates through each node in list here
            'Need to add unique nodes (grouped together) as child nodes of original Tree.
        Next
    End Sub

Let me know if I need to clarify something, thanks.

هل كانت مفيدة؟

المحلول

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top