Results 1 to 4 of 4
  1. #1
    Bojan Trpezanovski's Avatar
    Join Date
    Jan 2012
    Gender
    male
    Location
    Skopje, Macedonia
    Posts
    7
    Reputation
    10
    Thanks
    2
    My Mood
    Inspired

    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
    Last edited by Bojan Trpezanovski; 04-14-2012 at 07:39 AM.

  2. #2
    willrulz188's Avatar
    Join Date
    Mar 2010
    Gender
    male
    Location
    Ohio?
    Posts
    1,786
    Reputation
    35
    Thanks
    231
    My Mood
    Amazed
    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)
    Question ALL statements! ?
    You're in denial that you're in denial. ?
    [img]https://i360.photobucke*****m/albums/oo45/blood188/Untitled-3.jpg?t=1284590977[/img]

  3. The Following User Says Thank You to willrulz188 For This Useful Post:

    Bojan Trpezanovski (04-14-2012)

  4. #3
    Bojan Trpezanovski's Avatar
    Join Date
    Jan 2012
    Gender
    male
    Location
    Skopje, Macedonia
    Posts
    7
    Reputation
    10
    Thanks
    2
    My Mood
    Inspired
    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

  5. #4
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    Marked Solved. No more posts unless necessary.

Similar Threads

  1. [Question] Random number below a set number
    By That0n3Guy in forum C++/C Programming
    Replies: 4
    Last Post: 10-24-2009, 06:02 AM
  2. [TuT]Generate Random Number
    By Iamazn1 in forum Visual Basic Programming
    Replies: 1
    Last Post: 10-09-2009, 11:10 PM
  3. LOL got random message from random peep?
    By random_person in forum Spammers Corner
    Replies: 5
    Last Post: 08-25-2009, 01:13 AM
  4. RANDOM DISCONECTS FROM HACK NON VIP
    By killerkris14 in forum Combat Arms Hacks & Cheats
    Replies: 6
    Last Post: 01-02-2009, 08:56 AM
  5. Random Number Generator
    By Iam"iDude" in forum Visual Basic Programming
    Replies: 5
    Last Post: 10-02-2007, 04:16 PM