Results 1 to 3 of 3

Thread: [RESOLVED] Going through a listbox and removing all items that are the same.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [RESOLVED] Going through a listbox and removing all items that are the same.

    Hi there all! I am finishing up a game that demonstrates probability to students, and what I have is a listbox that is randomized, then the prog picks an item to add to a label caption. Then it removes that one item that was in the label caption. I was wondering if there was a way to remove all instances of whatever was in that label caption from the listbox?

    I use this to randomly pick an item and it works GREAT.

    VB Code:
    1. Public Sub PickAWinnerNOW()
    2. Dim lIndex As Long
    3.    If StudentList.ListCount Then
    4.       lIndex = Int(Rnd * StudentList.ListCount)
    5. 'This is where the program will add the item to the label caption
    6.       TicketDrawWinnerLBL.Caption = StudentList.List(lIndex)
    7.  
    8.   'Now have the program remove the item from the listbox
    9.       StudentList.RemoveItem lIndex
    10.    End If

    Now this will definitely remove the item, but as mentioned before I would like to remove all instances of that item. In an earlier thread, advice for a loop was given, which is great as a loop should go through the listbox and look/remove the label caption, but how do I set it up?

    Dim i As String (would it be a string since it is a name I am looking for)
    For i = 1 To CInt(TicketDrawWinnerLBL.Caption)
    StudentList.RemoveItem TicketDrawWinnerLBL.Caption
    Next

    I don't know if this is important to mention, but the list has by this point been mixed up, so the items are not all together, for ex: the name BOB would be mixed up with the other student names. Thanks for your help!!!

  2. #2
    gibra
    Guest

    Re: Going through a listbox and removing all items that are the same.

    Instead of

    Code:
    StudentList.RemoveItem lIndex


    removing
    should start by last to first item, not tested but should work:

    Code:
    On Error Resume Next
    For i =StudentList.ListCount -1 to 0 Step -1
        If StudentList.List(i) = TicketDrawWinnerLBL.Caption Then
           StudentList.RemoveItem i
       End If
    Next i

    Code:
    
    



  3. #3

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Going through a listbox and removing all items that are the same.

    Marvelous thank you!

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