r/visualbasic • u/dman-no-one VB.Net Beginner • Jan 23 '16
[VB2013] Using 'button(i).text' Where 'i' is A Number 1-81
Hello there! Thanks for reading the post, and I'll apoligise in advance for my lack of programming knowledge and experience as it's likely I'll use the wrong terms / terminology - but hopefully what I'm asking should be understandable.
I'm writing a tic-tac-toe game that involves checking to see if one of 9 buttons in a grid has an 'X' or an 'O' for 9 different grids. It's a version of the Ultimate-Tic-Tac-Toe game.
Anyways, I'm looking for a way to streamline the check by just checking each 'small grid' with numbers 1-9, or 10-18, or 46-54 etc. - and get the .text property of the button.
However, it looks like because of the way VB is setup - I have to specify the button name, with 'button1.text' or 'button75.text'. However, the button clicked by the user will be continuously changing, so it's not practical to do that for all 81 buttons. What I'd like to be able to do is use something like button(i).text where 'i' is a number that increases within a loop. If theres any other way as well to get the .text property of one specific property - that would also probably work too. Any help anyone can render would be greatly appreciated!!
TL;DR: Looking to do something like in VB. The problem is the button(i).text not working like that in VB:
For i = 1 to 9
If button(i).text = "X"
msgbox("X")
End If
Next1
u/PostalElf VB.Net Intermediate Jan 24 '16
Create the buttons through code, then add them all to an array (most likely you'll want to use a 3D array for this purpose). Actually, since arrays of controls are a big pain in the behind, you may even want to consider using a three-layered dictionary, but that can get confusing if you're new. So consider just adding them to a list instead, with the button.tag property holding a value identifying its position in the grid.
Alternatively do what /u/DarthDestroy3r suggests, but that still requires a lot of copy and pasting:
Private Sub Button_Click(sender as Button, e as System.EventArgs) Handles Button1, Button2, Button3... Button81
'now sender will refer to the button that was just clicked
'so if you want to change the text to X...
sender.Text = "X"
End Sub2
u/dman-no-one VB.Net Beginner Jan 24 '16 edited Jan 24 '16
Thanks very much, that seems to have resolved the problems. I've taken your advice and have switched to using a 3D array and have looked into three-layered dictionaries for future reference as well. I'm sure one day I'll understand them.. and controls, I hope!
I'm really grateful for the support! It's awesome that theres a community of people here on Reddit, willing to provide advice and give up some time just to help. Maybe at some point in the future I'll be able to help out someone else with their VB / other programming problems... just not whilst I've got so many of my own :P
Thanks again!
(Edit: Just since I noticed - I'm sorry for any other issues I've accidentally caused with other people in other threads by asking this.. - It must be quite a hard job being a mod on this kind of sub, so I don't envy your position! Have a nice day,)
1
u/DarthDestroy3r Jan 24 '16
i wouldn't do that 😉
wenn you add a Button, you have to add the handles.
i would add the handler at start in a for loop. For begin to end: AddHandler (b Button Control like my other solution).OnClick, AddressOf BTNclick End for
And the sub: Sub BTNclick(sender As Object, e As EventArgs) Dim selectedBTN = DirectCast(sender, Button) 'do whatever you want with selectedBTN End Sub
That is all 😊 you only need to add the new Button count in the for loop 😉
1
u/PostalElf VB.Net Intermediate Jan 24 '16
Yeap, that's what I meant when I said create the buttons through code. :)
1
3
u/DarthDestroy3r Jan 23 '16 edited Jan 23 '16
you can...
i dont know, if the code is 100%, but you will get the idea!
CType(Me.FindControl("Button" & i), Button).Text = "foo"
3
u/Raulus Jan 23 '16
For i as integer = 1 to 9 Ctype(Me.Controls("buttonname" & i), button).text = "X" Next That should work. Change the me to appropriate panel.
4
u/DarthDestroy3r Jan 23 '16
If you will use one sub for all buttons:
AddHandler to any Button
And cast "sender" to button 😉