|
-
Jan 15th, 2012, 10:47 AM
#1
Thread Starter
Member
Select word from list and display?
How do you randomly select a word from a list like below:
dog
cat
mouse
goldfish
pig
cow
and display it in a text box when a button is clicked, but where one word will not appear more than once each time the program is run? So, for example,
1st click: pig
2nd click: mouse
3rd click: dog
4th click: cow
etc. But NOT
1st click: pig
2nd click: dog
3rd click: mouse
4th click: pig
Any ideas?
-
Jan 15th, 2012, 12:39 PM
#2
Junior Member
Re: Select word from list and display?
You could do something like this
Code:
Public Class Form1
Dim randomList As New List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
randomList.Add("pig")
randomList.Add("dog")
randomList.Add("cat")
randomList.Add("mouse")
randomList.Add("goldfish")
End Sub
Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
Dim randomNumber As Integer = makeRandom()
If Not randomList.Count = 0 Then
Me.TextBox1.Text = randomList(makeRandom)
randomList.RemoveAt(makeRandom)
Else
Me.TextBox1.Text = " No Items to show"
End If
End Sub
Private Function makeRandom() As Integer
Dim randomGenerator As System.Random = New System.Random()
Return randomGenerator.Next(0, randomList.Count)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If randomList.Count > 0 Then
randomList.RemoveRange(0, randomList.Count)
End If
TextBox1.Text = ""
randomList.Add("pig")
randomList.Add("dog")
randomList.Add("cat")
randomList.Add("mouse")
randomList.Add("goldfish")
End Sub
End Class
-
Jan 15th, 2012, 01:00 PM
#3
Thread Starter
Member
Re: Select word from list and display?
Thanks. Exactly what I needed.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|