I have an array of strings and I would like to display 1 of the random values from the string array when the button is pressed. So when the "Vowels" button is pressed it displays 1 of the random vowels in the textbox.
1 Answer
You can use this method on the Button.Click event:
Public Class Form1
Dim rnd As New Random
Private Sub Button1_Click (ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
TextBox1.Text = strVowels(rnd.Next(strVowels.Length))
End Sub
End Class
(Thanks for the reminder of the limits of Random, @StevenDoggart!)
3 Comments
Steven Doggart
It's worth mentioning that the
Random object's randomization algorithm is seeded with the current time in its constructor, so, if you plan to call Next multiple times, it's best to only create the Random object once, such as in the Form.Load and reuse the same object each time.Viktor Ek
@Hassan I updated my code so you can easier see how to implement the example!