比较 2 个列表视图 parent 和 child 项并更改图像索引
Compare 2 listview parent and child items and change image index
Listview1、Listview2 比较 parent 和 child 项。
这些项目被命名为 some-name.zip a-z 并且 child 文件被加载到每个 parent.
Listview1 是目标
Listview2 是我加载的文件,我想与 Listview1
进行比较
image = 4 '绿色图标
image = 3 '红色图标
比较 2 listview,如果匹配则 listview2 image = 4 else image = 3
如果 child 匹配 then image = 4 else 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 控件并比较它们的内容。如果 Source TreeView 中的节点在 Target TreeView 中没有相应的节点,则其 Image
设置为 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!"
Listview1、Listview2 比较 parent 和 child 项。
这些项目被命名为 some-name.zip a-z 并且 child 文件被加载到每个 parent.
Listview1 是目标
Listview2 是我加载的文件,我想与 Listview1
进行比较
image = 4 '绿色图标
image = 3 '红色图标
比较 2 listview,如果匹配则 listview2 image = 4 else image = 3
如果 child 匹配 then image = 4 else 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 控件并比较它们的内容。如果 Source TreeView 中的节点在 Target TreeView 中没有相应的节点,则其 Image
设置为 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!"