Click to See Complete Forum and Search --> : Remove matching strings in listbox


mbw
September 27th, 2001, 10:55 AM
I have two list boxes. ListBox1 contains a random list of items, while ListBox2 contains a subset of ListBox1's items in no particular order. I want to remove all items in ListBox1 that match the items in ListBox2.

For example, ListBox1 contains items Harry, Sally, Mary, John. ListBox2 contains items Mary,John. I want to create a function that will remove Mary and John from ListBox1. Any help would be great! Thanks

Iouri
September 27th, 2001, 11:20 AM
Check this out. It is an example how to sync 2 lisyboxes


Private Sub btnAdd_Click()
List1.AddItem List1.ListCount + 1
List2.AddItem List2.ListCount + 1
End Sub

Private Sub btnDelete_Click()
'If no item selected quit
If List1.ListIndex = -1 Or List2.ListIndex = -1 Then Exit Sub
List1.RemoveItem List1.ListIndex
List2.RemoveItem List2.ListIndex
End Sub

Private Sub Form_Load()
Dim i As Integer, dy As Integer
'Add some items to the list boxes
For i = 1 To 6
List1.AddItem i
List2.AddItem i
Next i
End Sub

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim dy

'Delete the following line if you want the listbox to except rightmouse button click
If Button <> 1 Then Exit Sub
dy = Int(Y / TextHeight("A"))
'If cursor not on a list item quit
If dy + List1.TopIndex > List1.ListCount - 1 Then Exit Sub
'Select item
List1.ListIndex = dy + List1.TopIndex
List2.ListIndex = List1.ListIndex
End Sub

Private Sub List2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim dy
'Delete the following line if you want the listbox to except right
'mouse button click
If Button <> 1 Then Exit Sub
dy = Int(Y / TextHeight("A"))
'If cursor not on a list item quit
If dy + List2.TopIndex > List2.ListCount - 1 Then Exit Sub
'Select item
List2.ListIndex = dy + List2.TopIndex
List1.ListIndex = List2.ListIndex
End Sub



Iouri Boutchkine
iouri@hotsheet.com

MKSa
September 27th, 2001, 01:37 PM
private Sub DelMatchingItems_Click()
Dim i as Long
for i = 0 to List2.ListCount - 1
List2.ListIndex = i
List1.Text = List2.Text
If List1.ListIndex >= 0 then List1.RemoveItem List1.ListIndex
next
End Sub