VS 2010 Select word from list and display?-VBForums
Results 1 to 3 of 3

Thread: Select word from list and display?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    34

    Exclamation 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?

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    25

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    34

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.