Snippet - Select random listbox item
Posted: Sat Feb 15, 2014 2:06 pm
Hey guys,
Noticed the last snippet in this section was posted May LAST YEAR! So I decided to share some code I used for my latest program. I use it for a list of items, the items would be a list of games I want to play but not sure what, or a list of chores and I let the program decide for me.
Noticed the last snippet in this section was posted May LAST YEAR! So I decided to share some code I used for my latest program. I use it for a list of items, the items would be a list of games I want to play but not sure what, or a list of chores and I let the program decide for me.
Code: Select all
We store the amount of items from our list, then generate a random number between 0 and the amount of items, then we select that item. Can be useful for many things I'm sure. Randomize()
Dim count As Integer = lstbox1.Items.Count -1
Dim val As New Random
lstbox1.SelectedIndex = val.Next(0, lstbox1.Items.Count)