Click here to Skip to main content
15,882,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How can we write code for choosing random item, and displaying it through msgbox, from listbox when the listbox contains the selected item (that is of type string) from a combobox?

It should return always a different random item whenever the program is executed.


Thanks
Posted
Updated 7-Sep-10 21:28pm
v2
Comments
Dalek Dave 8-Sep-10 3:28am    
Edited for Grammar.

you can use random function.
in that supply count of list item in it.


From Jitendra Tikudia.

Please Do Vote .
 
Share this answer
 
Comments
Dalek Dave 8-Sep-10 3:32am    
It's what I would use.
May this solution satisfy you.

As you said you tried for integer type. So instead you use that random selection logic on index value of listbox items which is numeric.

You can then get value of selected index of listbox which can be any integer or string value.
 
Share this answer
 
v2
You should note that if it is random, you may get the same result a few times on the trot, for that is the nature of random.

Your insistance on it always being different would require an array containing the last N choices (where N is the number of items in the box), and not letting anything go unless it had not been selected by random chance. Once all had been, clear the array and start again.
 
Share this answer
 
VB
Private Sub btnGetRandomItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetRandomItem.Click
    Dim rnd As New Random
    Dim randomIndex As Integer = rnd.Next(0, ListBox1.Items.Count - 1)
    MessageBox.Show(ListBox1.Items(randomIndex))
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900