Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Serbia and Montenegro (Yugoslavia)
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Removing Multiple List Items (VB6)

    I have two list boxes on a form. I have it working to where when you double click on one listbox it removes it from the listbox clicked on and moves it over to the other. The other listbox works the same way, double click on a listitem and it moves it back to the first.

    I am now trying to do a multi select thing where you multi select listitems and click on a button, it moves them all over and then remove all selected items from the source listbox. Problem I'm running into (and I'm sure it is real basic and I'm probably not the first) is that when you remove a listitem, it alters the index order when trying to remove the next listitem. What is the proper way to do this?

    Here is the code for the on click event of the command button used to execute the move and remove:

    Private Sub cmdMoveOver_Click()


    Dim iCounter As Integer

    For iCounter = 0 To lstFrom.ListCount - 1

    If lstFrom.Selected(iCounter) = True Then

    lstTo.AddItem lstFrom.List(iCounter) ', lstFrom.ListIndex

    End If


    Next


    For iCounter = 0 To lstFrom.ListCount - 1

    If lstFrom.Selected(iCounter) = True Then

    lstFrom.RemoveItem lstFrom.ItemData(iCounter) ', lstFrom.ListIndex

    End If


    Next

    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Removing Multiple List Items (VB6)

    The solution is simple indeed: move backwards through the items.

    For iCounter = lstFrom.ListCount -1 To 0 Step -1
    ...
    Next iCounter

    Now, if you remove a list item, you won't be bitten by it.

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Removing Multiple List Items (VB6)

    And, as an aside, the same is true of collections.
    Charlotte

Posting Permissions

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