CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2001
    Location
    MA
    Posts
    11

    Remove matching strings in listbox

    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


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Remove matching strings in listbox

    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
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: Remove matching strings in listbox

    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





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured