2

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 1

3

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!)

Sign up to request clarification or add additional context in comments.

3 Comments

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 I get an error with that code that says "Expression is not a method" ?
@Hassan I updated my code so you can easier see how to implement the example!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.