Question Need button to recognize text entered in a textbox.

TooHighForThisShi

New member
Joined
Apr 28, 2013
Messages
2
Programming Experience
Beginner
I have an imagelist containing 7 images, with a click of a button one of those images randomly appears in a picturebox and the user has to type what the image is in a textbox, after that the user clicks on another button to check if the input entered in the textbox was correct.

I've done everything until the textbox part, how do I make it so that the check button recognizes the text entered in the textbox, and how to I link that to each picture in the imagelist?

Would appreciate any help. Thank you.
 
The Button doesn't recognise any text. The Button doesn't even know the TextBox exists. In order to do something when the Button is clicked, you need to handle its Click event handler. To do that, you will generally double-click the Button in the designer. When you do that, the code window opens and you see the generated Click event handler. That event handler is a method (Subs and Functions are all methods) and it's a member of the form, not the Button. When the user clicks the Button it raises its Click event, which invokes that method of the form. That's the sum total of what the Button does. Everything else is up to the form.

So, in order to get this all to work, the first thing you need to do is set up your ImageList correctly by providing a key for each Image. If you are adding the Images in the designer then the Name property of each one is its key. If you're adding them in code then you need to pass a key when calling Add, e.g.
VB.NET:
ImageList1.Images.Add("Chicken", Image.FromFile("Chicken.jpg"))
Now, to display a random Image you should create a Random object and generate a random number and use that as an index into the Images collection of the ImageList. You get the Image at that index and display it. Just make sure that you keep the index, which you would do by assigning it to a field.

When the user clicks the second Button, you call the IndexOfKey method of the Images collection of your Imagelist and pass the value they entered, i.e. the Text of the TextBox. If the index of the key matches the index of the Image you displayed then the user has correctly identified the Image, otherwise they have not. Note that IndexOfKey is case-insensitive so you don't have to worry about upper and lower case in your keys or the user's input.
 
The Button doesn't recognise any text. The Button doesn't even know the TextBox exists. In order to do something when the Button is clicked, you need to handle its Click event handler. To do that, you will generally double-click the Button in the designer. When you do that, the code window opens and you see the generated Click event handler. That event handler is a method (Subs and Functions are all methods) and it's a member of the form, not the Button. When the user clicks the Button it raises its Click event, which invokes that method of the form. That's the sum total of what the Button does. Everything else is up to the form.

So, in order to get this all to work, the first thing you need to do is set up your ImageList correctly by providing a key for each Image. If you are adding the Images in the designer then the Name property of each one is its key. If you're adding them in code then you need to pass a key when calling Add, e.g.
VB.NET:
ImageList1.Images.Add("Chicken", Image.FromFile("Chicken.jpg"))
Now, to display a random Image you should create a Random object and generate a random number and use that as an index into the Images collection of the ImageList. You get the Image at that index and display it. Just make sure that you keep the index, which you would do by assigning it to a field.

When the user clicks the second Button, you call the IndexOfKey method of the Images collection of your Imagelist and pass the value they entered, i.e. the Text of the TextBox. If the index of the key matches the index of the Image you displayed then the user has correctly identified the Image, otherwise they have not. Note that IndexOfKey is case-insensitive so you don't have to worry about upper and lower case in your keys or the user's input.

Thank you for the quick reply. Can you please elaborate on the indexofkey method and how to use it with the check button?

Edit: Is this any close to being correct or am I a moron?
VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text = "Apple" And PictureBox1.Image = ImageList1.Images("Apple") Then
            MessageBox.Show(TextBox2.Text, "Congratulations, you are Correct!")
            End
        Else
            MsgBox("Incorrect answer, press the Start button to retry", MsgBoxStyle.OkOnly)
            End
        End If

The PictureBox1.image section is obviously wrong but it's an example of what I wanted, basically if the image shown in the picture box is the apple and you type in apple in TextBox1, a message should appear in another (only readable) textbox below, and then I want to repeat the same thing for the other images. Like a quiz sort of app.

I realize the code above is for a messagebox but I can't get it to work for the textbox.
 
Last edited:
What did you not understand about the IndexOfKey method from what you found online and in the MSDN documentation when you searched? It's a function, you pass it a key and it returns you an index. It's not especially complex.
 
Back
Top