|
-
Jan 24th, 2013, 03:31 AM
#1
Thread Starter
PowerPoster
[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:
Public Sub PickAWinnerNOW()
Dim lIndex As Long
If StudentList.ListCount Then
lIndex = Int(Rnd * StudentList.ListCount)
'This is where the program will add the item to the label caption
TicketDrawWinnerLBL.Caption = StudentList.List(lIndex)
'Now have the program remove the item from the listbox
StudentList.RemoveItem lIndex
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!!!
-
Jan 24th, 2013, 03:59 AM
#2
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
-
Jan 24th, 2013, 04:06 AM
#3
Thread Starter
PowerPoster
Re: Going through a listbox and removing all items that are the same.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|