比较TreeViews并删除多余的项目

时间:2019-10-27 21:24:18

标签: vb6

Treeview1具有a-z邮政编码名称,每个zip都有子项。 Treeview2具有相同的zip名称,但是每个额外的不需要的子项都可能存在。我需要从Treeview2中删除多余的项目。

enter image description here

2 个答案:

答案 0 :(得分:0)

这是一个应该起作用的解决方案,具有有用的功能,可用于项目的后续版本:

Private Sub PruneTreeView(ByRef p_objSourceTreeView As TreeView, ByRef p_objTargetTreeView As TreeView)
    Dim objSourceNode As Node
    Dim objMatchNode As Node
    Dim objSourceChildNode As Node
    Dim objTargetChildNode As Node
    Dim fFound As Boolean
    Dim iNodeIndex As Integer
    Dim sSummary As String

    ' Build summary string
    sSummary = "Prune TreeView Summary:" & vbCrLf

    ' Get first node from Source TreeView
    Set objSourceNode = p_objSourceTreeView.Nodes(1)

    Do While Not objSourceNode Is Nothing

        ' Check if node has children, otherwise no need to look for match
        If objSourceNode.Children > 0 Then

            ' Find matching node in Target TreeView
            Set objMatchNode = GetMatchingNode(p_objTargetTreeView, p_objTargetTreeView.Nodes(1), objSourceNode)

            If Not objMatchNode Is Nothing Then

                sSummary = sSummary & "Source Node '" & objSourceNode.Text & "' Found in Target." & vbCrLf

                ' Check all children in Target Node
                If objMatchNode.Children > 0 Then

                    ' Set Found flag to False
                    fFound = False

                    ' Get first Child of Target
                    Set objTargetChildNode = objMatchNode.Child

                    Do While Not objTargetChildNode Is Nothing

                        ' Look for match in Source Tree
                        fFound = Not GetMatchingNode(p_objSourceTreeView, objSourceNode.Child, objTargetChildNode) Is Nothing

                        ' Keep Index reference
                        iNodeIndex = objTargetChildNode.Index
                        sSummary = sSummary & "Target Child Node '" & objMatchNode.Text & ":" & objTargetChildNode.Text & "'"

                        ' Go to next sibling
                        Set objTargetChildNode = GetNextSibling(p_objTargetTreeView, objTargetChildNode)

                        If fFound Then
                            sSummary = sSummary & " Found in Target." & vbCrLf
                        Else
                            ' No Match found
                            sSummary = sSummary & " Not Found in Target: Deleting at Index " & iNodeIndex & vbCrLf
                            p_objTargetTreeView.Nodes.Remove iNodeIndex
                        End If

                        DoEvents

                    Loop

                End If

            End If ' MatchNode exists

        End If ' Source Node Children > 0

        ' Go to next sibling
        Set objSourceNode = GetNextSibling(p_objSourceTreeView, objSourceNode)

    Loop

End Sub

以下是我编写的用于在查找节点时处理常见任务的Helper函数:

Function GetMatchingNode(ByRef p_objTreeView As TreeView, ByRef p_objStartNode As Node, ByRef p_objCompareToNode As Node) As Node
    Dim objNode As Node

    ' Get First Node
    Set objNode = p_objStartNode

    ' Check all Siblings and match on Text property
    Do While Not objNode Is Nothing
        If objNode.Text = p_objCompareToNode.Text Then
            ' Match found
            Set GetMatchingNode = objNode
            Exit Do
        Else
            Set objNode = GetNextSibling(p_objTreeView, objNode)
        End If
    Loop

    Set GetMatchingNode = objNode

End Function

Function GetNextSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Node
    If HasSibling(p_objTreeView, p_objNode) Then
        Set GetNextSibling = p_objTreeView.Nodes(GetNextSiblingIndex(p_objNode))
    Else
        Set GetNextSibling = Nothing
    End If
End Function

Function HasSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Boolean
    HasSibling = Not (p_objNode.LastSibling Is p_objNode)
End Function

Function GetNextSiblingIndex(ByRef p_objNode As Node) As Integer
    With p_objNode
        GetNextSiblingIndex = .Index + .Children + 1
    End With
End Function

答案 1 :(得分:0)

所有学分归于ÉtienneLaneville

 Private Sub PruneTreeView(ByRef p_objSourceTreeView As TreeView, ByRef p_objTargetTreeView As TreeView)
            Dim objSourceNode As Node
            Dim objMatchNode As Node
            Dim objSourceChildNode As Node
            Dim objTargetChildNode As Node
            Dim fFound As Boolean
            Dim iNodeIndex As Integer
            Dim sSummary As String

            ' Build summary string
            sSummary = "Prune TreeView Summary:" & vbCrLf

            ' Get first node from Source TreeView
            Set objSourceNode = p_objSourceTreeView.Nodes(1)

            Do While Not objSourceNode Is Nothing

                ' Check if node has children, otherwise no need to look for match
                If objSourceNode.Children > 0 Then

                    ' Find matching node in Target TreeView
                    Set objMatchNode = GetMatchingNode(p_objTargetTreeView, p_objTargetTreeView.Nodes(1), objSourceNode)

                    If Not objMatchNode Is Nothing Then

                        sSummary = sSummary & "Source Node '" & objSourceNode.Text & "' Found in Target." & vbCrLf

                        ' Check all children in Target Node
                        If objMatchNode.Children > 0 Then

                            ' Set Found flag to False
                            fFound = False

                            ' Get first Child of Target
                            Set objTargetChildNode = objMatchNode.Child

                            Do While Not objTargetChildNode Is Nothing

                                ' Look for match in Source Tree
                                fFound = Not GetMatchingNode(p_objSourceTreeView, objSourceNode.Child, objTargetChildNode) Is Nothing

                                ' Keep Index reference
                                iNodeIndex = objTargetChildNode.Index
                                sSummary = sSummary & "Target Child Node '" & objMatchNode.Text & ":" & objTargetChildNode.Text & "'"

                                ' Go to next sibling
                                Set objTargetChildNode = GetNextSibling(p_objTargetTreeView, objTargetChildNode)

                                If fFound Then
                                    sSummary = sSummary & " Found in Target." & vbCrLf
                                Else
                                    ' No Match found
                                    sSummary = sSummary & " Not Found in Target: Deleting at Index " & iNodeIndex & vbCrLf
                                    p_objTargetTreeView.Nodes.Remove iNodeIndex
                                End If

                                DoEvents

                            Loop

                        End If

                    End If ' MatchNode exists

                End If ' Source Node Children > 0

                ' Go to next sibling
                Set objSourceNode = GetNextSibling(p_objSourceTreeView, objSourceNode)
        DoEvents
            Loop

        End Sub