Results 1 to 5 of 5

Thread: pick a random?

  1. #1
    digitalengineer09 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Posts
    3
    Reputation
    0

    Exclamation pick a random?

    Hey Guys,
    Im not so good at VB.NET , and ive been learning for about 3 months. I would really appreciate it if you guys would help me tackle this problem down.

    Well , im helping a school run a Spelling Bee aimed at children in year 7. And to make it fair and unbiased i need an application that can choose randomise a word from the list box then transfer the string to the text box.

    So basically something that can pick a random string from the listbox and show it in the textbox1.text property.

    Once again , Thanks in advance. I would love it if you guys could help me with this problem. I cant seem to do it with the skills i currently have.

    Bye,
    Digital Engineer2010

  2. #2
    picoflop is offline VB.NET Forum Genius
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Feb 2009
    Posts
    158
    Reputation
    131
    Use the Random() class and its Random.Net(lowervalue_inclusive, uppervalue_exclusive) method to determine a list entry. Remove entry from listbox and store in textbox.

  3. #3
    digitalengineer09 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Posts
    3
    Reputation
    0
    Man i still dont understand. If someone could help me i would really apreciate it . i have got a list box a button and a label in the gui part.
    Thanks Guys!

  4. #4
    digitalengineer09 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Posts
    3
    Reputation
    0

    Fixed

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim r As New Random

    ListBox1.SelectedIndex = r.Next(0, ListBox1.Items.Count - 1)
    TextBox1.Text = ListBox1.Text
    ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
    End Sub

  5. #5
    picoflop is offline VB.NET Forum Genius
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Feb 2009
    Posts
    158
    Reputation
    131
    You have to use ListBox1.Items.Count because the upper boundary is EXCLUSIVE.
    i.e. r.next(1,6) will generate numbers from 1 to 5 and NOT up to 6

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking