tags:

views:

167

answers:

2

I am making a game of tic tac toe, and I have 9 buttons lined up in a grid pattern. I want to select a random button for the computer to start the game from.

I have an array set up with all the names of my buttons, and I was thinking of picking a random entry from that array to start working from. This I have done fine, but I cannot change the text of a button. My code:

''# Define the array
random(0) = "tl"
random(1) = "tc"
random(2) = "tr"
random(3) = "cl"
random(4) = "cc"
random(5) = "cr"
random(6) = "bl"
random(7) = "bc"
random(8) = "br"
''# Grab a random array entry
StartPoint = random(RandomClass.Next(0, 8))

as you can see, I can't simply do StartPoint.Text = "O", even thought StartPoint holds the name for the button.

Any help on changing the buttons text from the name in StartPoint would be helpful, thanks.

+4  A: 

You should create an array of actual buttons (not their names). Then when you grab a random button into a button object, it'll actually be a button so you can change it's text property.

Since you're just passing around references to the actual buttons, this should work pretty well.

Dim buttons(8) As Button
buttons(0) = tl
buttons(1) = tc
''# ...
Michael Haren
Thanks for the suggestion, could you tell me how to do that exactly? I'm pretty sure I need to define the array as something other than string but I'm not sure what it should be. Thanks.
Willis
I edited in an example
Joel Coehoorn
Thanks, Joel, appreciated
Michael Haren
A: 

Why don't you create an array of Button objects?

That way, all you have to do is cast access them and set the Text property.

Button startButton = random(RandomClass.Next(0,8))
startButton.Text = "o"
Wim Hollebrandse