Selecting a random button and modifying its text (VB.NET) - vb.net

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.

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
''# ...

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"

Related

Giving buttons different names through code and referring to them later on

My code creates a 5x5 grid of buttons. I am wanting to give each of these buttons different names "BtnColour1", "BtnColour2", etc. How do I give them all different names and how do I refer to each button later in the program?
Dim bytCounter As Byte
For bytCounter = 1 To 25
Dim btnColour As New Button
Me.Controls.Add(btnColour)
btnColour.Height = 50
btnColour.Width = 50
btnColour.Name = "btnColour" & bytCounter
btnColour.Enabled = False
btnColour.Left = ((bytCounter - 1) Mod 5) * 51
btnColour.Top = ((bytCounter - 1) \ 5) * 51
AddHandler btnColour.Click, AddressOf BtnClick
Your code (I guess you forgot the ending Next) does create 25 Buttons, with names btnColour1... btnColour25.
In the BtnClick event, to get the name of the clicked button, you should write something like:
Private Sub BtnClick(sender As Object, e As EventArgs)
Dim buttonName as string=CType(sender, Button).Name
'buttonName now has the clicked button name
End Sub
Of course, since you set the enabled property to False, your button click event will not fire.
In a general sense (and in addition to Spyros' answer, which is a good way to do it in an event handler - the sender is always the thing that raised the event), when you give a control a name and add it to a control's Controls collection, you can then retrieve it by that name later:
'Here you added the button to the form controls:
Me.Controls.Add(btnColour)
'later in the code you can ask for it back by name, for example:
Dim controls = Me.Controls.Find("btnColour1")
What you get back is an array of Controls. You get an array because Find can search all children (panels inside panels inside groupboxes inside forms etc) and it is thus conceivable that multiple controls in different panels will both have the same name. In your case if you know you only have one control called "btnColour1" it's safe to get it by array index:
Dim control = controls(0) 'controls variable is from the above Find
Lastly, remember that it comes back as a Control, the parent class for all controls. Because you know it's a button, it's safe to cast without check:
Dim button = DirectCast(control, Button)
Remember that if your property is available on the base Control class you don't even need a cast:
'here's a 1 line way to get the text of the button named btnColour1
'Find all controls named btnColour1, take the first, get the text
Dim t = Me.Controls.Find("btnColour1")(0).Text
If you want to refer to the buttons later in the program to change a setting without clicking the button, you can add each button to an array and call each of them with an index number:
Dim buttons(24) As Button
Then as the buttons are created, you can add each button to the array:
Dim bytCounter As Byte
For bytCounter = 1 To 25
Dim btnColour As New Button
Me.Controls.Add(btnColour)
buttons(bytCounter) = btnColour
you can then reference each button and their properties using the index number of the button in the array. You may also want to add a specific tag to each button to make each button more unique using:
btnColour.Tag = bytCounter

Is there anyway to save a multiple buttons as a list?

I am making small restaurant system project. I have 16 buttons as tables in the restaurant. I would like to change their colors or disable any of them when some events trigger (the form is loaded).
I save my button names in format TableX_ButtonY
I used a for-loop to change theirs border colors like this:
CType(Me.Controls.Find(String.Format("Table{0}Button{1}", i, x), True)(0), Button).FlatAppearance.BorderColor = Color.Blue
It will be great if I can save these buttons as a list, so I can manage it more easily.
I name their tags from 1 to 16 but I don't know how to use them correctly. Because the trigger not based on button click but rather based on the Load Form event.
Buttons are already in a collection and is a bit redundant to add them to a generic collection. In this example there are 2 buttons in a group controls collection, which could very well be any applicable container.
Dim ReservedTables() As Integer = {5, 10, 15, 20}
For Each Btn As Button In GroupBox1.Controls.OfType(Of Button)
If ReservedTables.Contains(CType(Btn.Tag, Integer)) Then
Btn.Enabled = False
End If
Next
Dim TableList As New List(Of Button)
TableList.Add(TableX_ButtonY)
For Each Table As Button in TableList
'do stuff
next
if you generated the buttons by using the designer, you can use the method you described in your question to add them all to the list

Access VBA - Hide Labels, text box, buttons If = " "

I'm trying to hide make some labels, text boxes and buttons hidden:
If rst![RI] = "" Or IsNull(rst![RI]) Then
I have:
A Label named "Label83"
A Text box named "C1"
A Text box named "Tex4"
A Text box named "Text8"
A button named "Command18"
So whenever I'm on PM200 and If rst![RI] = "" Or IsNull(rst![RI])
Then the listed label, text boxes and button should be hidden.
Thank you in advance.
You can use as a minimum:
Me!Label83.Visible = Len(Nz(rst![RI].Value))
or, to play nice:
Me!Label83.Visible = CBool(Len(Nz(rst![RI].Value)))
For more controls, set a variable:
Dim Visible As Boolean
Visible = CBool(Len(Nz(rst![RI].Value)))
Me!Label83.Visible = Visible
Me!text8.Visible = Visible
' etc.
And do rename your controls to something meaningful, like: lblCtransfer

Reversi VB.net logic behind it

I am trying to make the reversi game in VB.Net. I have some difficulties translating the game`s logic into vb.net
If a button is black and the button next to it is white,than the button next to the white one will be black wen pressed.
newButton.tag = colum of button + (row of button * amount of columns)
-> I made 64 buttons via a function loop and added a tag
Dim knop As Button = sender
Dim value As String = knop.Tag
If value = "...(?)" Then
knop.BackColor = Color.Black
If ....(?)
End If
End If
I already made a scheme with the label of the buttons, but I find it hard to implement the logic. Can someone help me out with thid one?
EDIT: http://i.stack.imgur.com/3gdrJ.png
If you use Dim ButtonList As List(Of List(Of Button)) and add the buttons to the form in runtime you can add each the button for each row to a list then add that list to ButtonList. Now you can access each button by the indexes in the 2 dimensional list.
Since you're changing the backcolor just use that instead of using the tag.

Can I get a random button to blink in vb.net?

I'm creating a game where you have to click buttons in a certain sequence.
I want the buttons to blink in order at the beginning so the player could take a look at the sequence and memorize it..
on each round, the buttons get assigned new values, hence the order changes,
I want to be able to make any button blink, meaning instead of writing button1, I wanna be able to use button(i) (if it's possible)
thanks in advance!
You can put all your buttons that should blink into a list, then create a random number, and use this random number to get the button at this index.
Dim buttons = new Button() {button1, button2, button3} 'Put buttons into list
Dim r = new Random()
Dim seq_length = 3 'Let three buttons blink
For i = 0 To seq_length
Dim index = r.Next(0, buttons.Count()) 'Get random index
LetButtonBlink(buttons(index)) 'Use this index to select a button
Next