Resolved Store a variable

anybloodyid

Member
Joined
Jan 25, 2018
Messages
16
Programming Experience
Beginner
Hi and thanks for looking,
I followed a tutorial at microsoft on making a simple matching game after I finised I then started to adapt it so I could have a matching times table.
I have two TableLayoutPanels the first is populated with the questions 2 x 2 = etc
The second is populated with the answers 2, 4, 6, etc
The problem I have is when I click a question eg 2 x 4 = and then click the answer eg 8 it doesn't stay visible obviously these two lines of text do not match.

If firstClicked.Text = secondClicked.Text Then firstClicked = Nothing secondClicked = Nothing Exit Sub End If

How can I add the answer to the question (not seen by player) but can be checked if it matches secondClicked.text.
Below is how I assignquestions to squares
Private Sub AssignQuestionsToSquares() ' The TableLayoutPanel has 12 labels, ' and the questions list has 12 questions, ' so an question is pulled at random from the list ' and added to each label For Each control In TableLayoutPanel1.Controls Dim questionLabel = TryCast(control, Label) If questionLabel IsNot Nothing Then Dim randomNumber = random.Next(questions.count) questionLabel.Text = tableNumber & " x " & questions(randomNumber) & " = " questionLabel.ForeColor = questionLabel.BackColor questions.RemoveAt(randomNumber) End If Next End Sub
 
use the Tag property to hold the answer.
Use the label tag property:
questionLabel.Tag = tableNumber * randomNumber

Then in your matching code, check the Tag property of the times table selection with the text of the answers.
 
I used TabIndex
questionLabel.TabIndex = tableNumber * questions(randomNumber)
Then checked with
If secondClicked.Text = firstClicked.TabIndex Then
 
Hmmm, Tag is a property specifically created to handle a secondary descriptor. TabIndex is the order in which the label is given focus when the Tab key is pressed and isn't meant to hold anything but that.
As long as that doesn't interfere with the value that TabIndex can hold (Int32) you should be okay using it.
 
I used TabIndex
questionLabel.TabIndex = tableNumber * questions(randomNumber)
Then checked with
If secondClicked.Text = firstClicked.TabIndex Then

That is a very, very bad solution. The TabIndex property has a specific purpose. If you change those values then, if a user uses the Tab key to navigate your form, the caret will jump around differently at different times. You should be using the Tag property and, if you can't, it's because you're doing something wrong, so you should work out what that is and fix it. Alternatively, you could even create a custom control that inherits the standard type and adds your own property specifically for the purpose. It's a few lines of code and then you can use it just like any other control.
 
It's just a property like any other. If you can set TabIndex then you can set Tag. Just note that it is type Object, because you can assign anything to it, so you should cast as type Integer when you get the value out if you put an Integer in. That will be required if you have Option Strict On, which you should.
 
Back
Top