SolvedPicking A Random Number From ListBox

Posts 1–4 of 4 · Page 1 of 1
Picking A Random Number From ListBox
Hi guys i need your help i have a code that randomly generates thigs from listbox
The code is:
Code:
Dim rnd As New Random
          Dim randomIndex As Integer = rnd.Next(0, ListBox1.Items.Count - 1)
          subjectbox.Text = ListBox1.Items(randomIndex)
Thats the code but i need when randomly pick items from listbox to select that item in the listbox(highlight it)
I need that becouse i will import this code after is selected to split the strings from the selected item from the listbox to 2 textboxes
Code:
Dim str, str1() As String
        str = ListBox1.SelectedItem.ToString
        str1 = Strings.Split(str)
        TextBox4.Text = str1(0)
        TextBox5.Text = str1(1)
So please tell me a code that randomly pick item from the lisrbox and selects the same item in the listbox
Imageshack - capturekll.png

Please help
Well here is one way

Code:
 Public Function RandomNumber(ByVal MaxNumber As Integer, _
       Optional ByVal MinNumber As Integer = 0) As Integer

        'initialize random number generator
        Dim r As New Random(System.DateTime.Now.Millisecond)

        If MinNumber > MaxNumber Then
            Dim t As Integer = MinNumber
            MinNumber = MaxNumber
            MaxNumber = t
        End If

        Return r.Next(MinNumber, MaxNumber)

    End Function
Then the event
Code:
Dim i = RandomNumber(ListBox1.Items.Count, 0)
        ListBox1.SetSelected(i, True)
God bless you my friend
this is how it worked out
Dim i = RandomNumber(ListBox3.Items.Count, 0)
ListBox3.SetSelected(i, True)
ListBox3.SelectedItem = TextBox1.Text
Marked Solved. No more posts unless necessary.
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us