Compare and remove unmatched items from list1 and treeview

Viewed 43

I have list1, treeview, list2

list1 has items

treeview1 has items list2 empty or has items populating over time

Now I want to loop through list1 compare it with list2 and if items do not match then remove the items from treeview1,list1.

The small problem I am having now is list1 ends up keeping 1 unwanted item.

list1 treeview1 and list2 over time has same items populated but list2 sometimes may not contain the same value as list1 this is why I need to loop and match strings. If not found then remove the strings from list1 and treeview1 the same items only.

This code below you can paste it into fresh vb6 project just add
1 treeview1 list1 and list2 and 2 command button

Private Sub Command1_Click()
List1.AddItem "dezzzzz"
TreeView1.Nodes.Add , , , "dezzzzz" & "/" & "General"

List1.AddItem "tammy8123"
TreeView1.Nodes.Add , , , "tammy8123" & "/" & "General"

List1.AddItem "sarah7232"
TreeView1.Nodes.Add , , , "sarah7232" & "/" & "General"



List2.AddItem "tammy8123"

End Sub

Private Sub Command2_Click()
Dim i As Integer, ii As Integer
Dim iii As Integer, demopacket() As String
For ii = List1.ListCount - 1 To 0 Step -1

Dim lstUserspacket() As String
lstUserspacket() = Split(List1.List(ii), "/")

For iii = 1 To TreeView1.Nodes.Count
demopacket() = Split(TreeView1.Nodes(iii).Text, "/")

For i = List2.ListCount - 1 To 0 Step -1

Dim listpacket() As String
listpacket() = Split(List2.List(i), "/")


If listpacket(0) = demopacket(0) Then
Exit For
End If
Next

 If i = -1 Then
On Error Resume Next
List1.RemoveItem ii


TreeView1.Nodes.Remove iii

End If


Next
Next

End Sub

Private Sub Form_Load()
Call Command1_Click
End Sub
1 Answers

My first thought was to simply find the issue with the code you provided. My next thought was realizing the code was too complex for a rather simple task. The code could be as simple as:

Private Sub Command2_Click()
   Dim i As Integer

   For i = List1.ListCount - 1 To 0 Step -1
      If Not ExistsInList(List2, List1.List(i)) Then
         TreeView1.Nodes.Remove FindInTree(TreeView1, List1.List(i))
         List1.RemoveItem FindInList(List1, List1.List(i))
      End If
   Next
End Sub

Of course, the controls in question do not provide an Exists or Find method. But it is not a hard task to create you own. If you place the following functions in a Utility module then you can use them in other projects, too:

Public Function ExistsInList(ByVal ListBox As ListBox, ByVal Item As String) As Boolean
   Dim i As Integer
   
   For i = 0 To ListBox.ListCount - 1
      If ListBox.List(i) = Item Then
         ExistsInList = True
         Exit Function
      End If
   Next
End Function

Public Function FindInList(ByVal ListBox As ListBox, ByVal Item As String) As Integer
   Dim i As Integer
   
   For i = 0 To ListBox.ListCount - 1
      If ListBox.List(i) = Item Then
         FindInList = i
         Exit Function
      End If
   Next
End Function

Public Function FindInTree(ByVal TreeView As TreeView, ByVal Item As String) As Integer
   Dim i As Integer
   
   For i = 1 To TreeView.Nodes.Count
      If InStr(1, TreeView.Nodes(i).Text, Item) > 0 Then
         FindInTree = i
         Exit Function
      End If
   Next
End Function
Related