select random button

solstice

New member
Joined
Feb 21, 2007
Messages
3
Programming Experience
Beginner
First off is this the correct post spot for new questions?

My delema is this. I am creating a simple game in which one button is pressed and then a second auto selects another button at random. I am almost certain that I need to use the random class but not sure. If needed I can send what I have so far.

Thank you in advance for any help anyone can offer.
 
hey and welcome
I think i can help...

How many buttons do you have?
do u want themto actually be 'clicked on' or just set focus to?

Kris.
 
There are multiple ways to do this. In this first example, I use the rnd function to generate a random number between 0 and 2, then use a select case statement to select the buttons.
VB.NET:
Dim MyValue As Integer
        MyValue = CInt(Int((3 * Rnd()) + 0)) ' Generate random value between 0 and 2.
        Select Case MyValue
            Case 0
                Me.Button2.Select()
            Case 1
                Me.Button3.Select()
            Case 2
                Me.Button4.Select()
        End Select

You should also be able to add the buttons to some kind of array or arraylist, and use the rnd function to randomly select an index of the array, then select that button. I would include an example, but It's late and I'm lazy. I hope this helps.
 
Back
Top