Vb6 比较2个listview父项和子项并更改图像索引

Vb6 比较2个listview父项和子项并更改图像索引,vb6,Vb6,Listview1和Listview2比较父项和子项。 这些项被命名为some-name.zip a-z,子文件加载到每个父项中 列表视图1是目标 Listview2是我要与Listview1比较的加载文件 image=4“绿色图标 image=3”红色图标 比较2个listview,如果匹配,则listview2 image=4,否则image=3 如果子项匹配,则image=4,否则image=3 startt = True Dim FoundIt As Boolean, ii As Int

Listview1和Listview2比较父项和子项。
这些项被命名为some-name.zip a-z,子文件加载到每个父项中

列表视图1是目标
Listview2是我要与Listview1比较的加载文件
image=4“绿色图标
image=3”红色图标

比较2个listview,如果匹配,则listview2 image=4,否则image=3
如果子项匹配,则image=4,否则image=3

startt = True
Dim FoundIt As Boolean, ii As Integer, ix As Integer
Dim NodX As Node, NodX2 As Node
For Each NodX In TreeView1.Nodes
ix = NodX.Index
For Each NodX2 In TreeView2.Nodes
ii = NodX2.Index
If NodX.FullPath = NodX2.FullPath Then
FoundIt = True
On Error Resume Next
Exit For
End If
DoEvents
'pause 0
If TreeView2.Nodes(ii).Index = TreeView2.Nodes.Count - 0 Then
Exit For
Exit Sub
End If
Next
If FoundIt Then
TreeView1.Nodes(ix).Image = 4
TreeView2.Nodes(ii).Image = 4
Else
TreeView2.Nodes(ix).Image = 3
End If
If TreeView2.Nodes(ii).Index = TreeView2.Nodes.Count - 0 Then
DoEvents
'Call Command16_Click
If downnn = True Then
Exit For
Exit Sub
End If
End If
FoundIt = False
Next
End Sub

下面是一个Sub,它接受两个TreeView控件并比较它们的内容。如果源树视图中的节点在目标树视图中没有对应的节点,则其
图像
将设置为3。否则,
Image
设置为4:

Private Sub CompareTreeViews(ByRef p_objSourceTreeView As TreeView, ByRef p_objTargetTreeView As TreeView)
    Dim objSourceNode As Node
    Dim objTargetNode As Node
    Dim objMatchNode As Node
    Dim objSourceChildNode As Node
    Dim objTargetChildNode As Node
    Dim iSourceCounter As Integer
    Dim iTargetCounter As Integer
    Dim fFound As Boolean
    Dim fChildrenMatch As Boolean

    On Error Resume Next

    For Each objSourceNode In p_objSourceTreeView.Nodes

        ' Reset ChildrenMatch flag, used to track if all children match
        fChildrenMatch = True

        ' Find matching node in Source TreeView
        For Each objTargetNode In p_objTargetTreeView.Nodes

            If objTargetNode.Text = objSourceNode.Text Then
                ' Match found
                Set objMatchNode = objTargetNode
                Exit For
            End If
        Next

        If Not objMatchNode Is Nothing Then

            ' Check all children
            If objSourceNode.Children > 0 Then

                ' Get first Child and Loop through all Children
                Set objSourceChildNode = objSourceNode.Child
                For iSourceCounter = 1 To objSourceNode.Children

                    ' Check if it exists in Target Treeview
                    If objMatchNode.Children > 0 Then

                        ' Set Found flag to False
                        fFound = False

                        ' Get first Child and Loop through all Children
                        Set objTargetChildNode = objMatchNode.Child
                        For iTargetCounter = 1 To objMatchNode.Children

                            ' Check for match
                            If objTargetChildNode.Text = objSourceChildNode.Text Then
                                fFound = True
                                Exit For
                            End If

                            ' Get next node
                            Set objTargetChildNode = objTargetChildNode.Next

                        Next

                        ' Mark Node
                        Select Case fFound
                            Case True
                                objSourceChildNode.Image = 4
                            Case False
                                objSourceChildNode.Image = 3
                                fChildrenMatch = False
                        End Select

                        ' Get next node
                        Set objSourceChildNode = objSourceChildNode.Next

                    End If

                    DoEvents

                Next ' Source Child

            End If

        End If

        Select Case fChildrenMatch
            Case True
                objSourceNode.Image = 4
            Case False
                objSourceNode.Image = 3
        End Select

        DoEvents

    Next ' Source Node

End Sub
根据您的问题,您希望通过以下方式调用Sub:

Label1.Caption = "Comparing..."
CompareTreeViews TreeView2, TreeView1
Label1.Caption = "Done!"

您能否确认以下内容:检查TreeView 2中的每个节点和子节点,并检查TreeView中是否有相应的节点。如果有,则设置Image=4,否则设置Image=3。我觉得我们昨天就这么做了!上面的代码可以工作,但是它的编程代码很俗气,它可以工作,但是油腻的代码不专业,你编写代码的方式看起来很专业,工作起来很流畅。代码中的一些缩进会有助于可读性。很好,我忘了提到两件事,如果所有子图像均为绿色,则父图像为绿色=4,或者如果子图像为绿色和红色,则父图像为3,并为Listview1添加相同的图像索引。另外,如果您可以添加一个状态到label1进程完成,以便我知道它已完成?我不知道您是如何做到这一点的,但我使用了一个名为fChildrenMatch的变量,该变量跟踪所有子项是否匹配。另外,就显示状态而言,您可以在调用
CompareTreeViews
之前和之后进行显示,如回答中所示。