Selecting a Random items from a listbox.
I wanted to make a program that enables the user to input text into a listbox and hit a button and it will randomly select one of the text out of however items there are. It will then take the random selected item and put it in another textbox.
I am attempting to use the Randomize() under the form but after a bit of messing around It wont convert string to single so is there a way to do this. If so please comment ASAP.. You guys are so helpful!!
Here are some of the things i tryed
Code:
Select Case LBRnd.Text
Case Rnd() = CStr(LBRnd.Text)
End Select
TextBox1.Text =
Code:
Select Case LBRnd.Text
Case Rnd(LBRnd.SelectedItems)
End Select
TextBox1.Text =
I'm not going to lie, in this field of coding. I am weak. I am more intuitive in logic coding.
Re: Selecting a Random items from a listbox.
Randomize + Rnd are legacy code. use the Random class, it's much simpler:
Code:
Public Class Form1
Dim r As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text = ListBox1.GetItemText(ListBox1.Items(r.Next(0, ListBox1.Items.Count)))
End Sub
End Class
Re: Selecting a Random items from a listbox.
Er, yes. Not the way you'd use Select Case in any event.
Dim r As New Random ' at the head of the form
TextBox2.Text = ListBox1.Items(r.Next(0, ListBox1.Items.Count)).ToString