Randomly choosing items from a listbox???

dapice57

New member
Joined
Jun 9, 2005
Messages
1
Programming Experience
Beginner
Hello All,

I'm trying to create a lottery program where I load a group of customer names into a listbox from a file and then I want to randomly choose a name from the listbox and display it in a label. I was able to get it using:

Label1.Text = lstcustomers.Items(Int(Rnd() * pintcount))

but that generates the same names when you reload the file and begin the random choosing. So the first name to come up is always the same once I exit and reload the program and the file.

I then found a Randomobject Function:

Public Function GetRandomNumber(ByVal Low As Integer, ByVal High As Integer) As Integer

' Returns a random number,

' between the optional Low and High parameters

Return objRandom.Next(Low, High + 1)

End Function

And I can get that to generate in the label fine as numbers.

My question is how do I link those random numbers to the items listed in the listbox?

Any help would be greatly appreciated.
 
VB.NET:
Expand Collapse Copy
Dim RandomNumber as New Random

Listbox1.SelectedIndex = RandomNumber.Next(0, Listbox1.Items.Count - 1)

Label1.Text = Listbox1.Text

should do the trick
 
Back
Top