Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random selection from list?
#1
http://webcache.googleusercontent.com/se...t5264.html

How would i do this in vb2010?
Reply
#2
This?
Quote:I'm trying to create a program that is a contest. Where you enter names in a input box and they are stored in the listbox. After the names are stored, you click a button and it picks a name at random from the listbox and displays the winner's name in a label. Any ideas? -question by Sherri

Add two buttons, a textbox, and a listbox.
Button1 will add the name in text box 1 to the list box
Button2 will display a message box with the answer

Paste this code.

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add(TextBox1.Text)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim rand As New Random
        MessageBox.Show(ListBox1.Items(rand.Next(0, ListBox1.Items.Count - 1)))
    End Sub

This should work for you. If you don't understand what it's doing, let me know and i will break it down and comment it. Its better that you know what's going on, then just copy/paste this to your project. That way, it will sink in and help you later down the road.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [How to] Create random strings(upper,lower and integers)[Src] Modestep 11 2,948 05-14-2011, 02:17 AM
Last Post: モrainee
  [Snippet] Random pick from list (Range) euverve 0 693 05-12-2011, 08:22 AM
Last Post: euverve
  [Tut] Generating Random Strings (200 Random Results = LIMIT) [Tut] Resistance 7 1,860 04-12-2011, 03:18 PM
Last Post: Resistance
  Generate Random Numbers & Letters [ Small Function ] MYPE 22 4,162 04-02-2011, 05:25 PM
Last Post: Pyratepig
  Need help with generating random fixtures Fragma 6 1,764 03-15-2011, 12:35 PM
Last Post: Fragma

Forum Jump:


Users browsing this thread: 2 Guest(s)