aarti
aarti

Reputation: 11

Issue with some Visual Basic programming code

Basically I have to design a fruit game where a random fruit will be generated and the child has to guess the fruit type. If the child guesses it right, they will receive a message saying congrats and if they get it wrong, they will receive a message saying "try it again".

I have done the programming but I don't know where I am going wrong when I type the name of the fruit. Because even though it is right, it gives a message saying it is wrong.

I have the program code for when the fruit is right and the message with it.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button1.Visible = True ' The tools that will need to be hide when the check button is cliked
        Button2.Visible = False
        PictureBox1.Visible = False
        TextBox1.Visible = False
    If Label1.Text = "1" And TextBox2.Text = "Banana" Then ' If both are true then the following result is true
        PictureBox2.Image = My.Resources.Well_done 'my.resources.name 'The well_done picture that appears for the correct
        PictureBox2.Visible = True                                        '- answer and at the same time it has to be visble
        Label2.Text = "Congrats! " & TextBox2.Text & "! Correct answer!" 'The msg which appears for the correct answer
        Label2.Visible = True
        Me.BackColor = Color.Yellow      ' The background colour of the form 
    ElseIf Label1.Text = "2" And TextBox1.Text = "apple" Then 'Similary for apple banana and other fruits
        PictureBox2.Image = My.Resources.Well_done
        PictureBox2.Visible = True
        Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
        Label2.Visible = True
        Me.BackColor = Color.Green
    ElseIf Label1.Text = "3" And TextBox1.Text = "orange" Then
        PictureBox2.Image = My.Resources.Well_done
        PictureBox2.Visible = True
        Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
        Label2.Visible = True
        Me.BackColor = Color.Orange
    ElseIf Label1.Text = "4" And TextBox1.Text = "Strawberry" Then
        PictureBox2.Image = My.Resources.Well_done
        PictureBox2.Visible = True
        Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
        Label2.Visible = True
        Me.BackColor = Color.IndianRed
    ElseIf Label1.Text = "5" And TextBox1.Text = "Grapes" Then
        PictureBox2.Image = My.Resources.Well_done
        PictureBox2.Visible = True
        Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
        Label2.Visible = True
        Me.BackColor = Color.Green

Upvotes: 0

Views: 449

Answers (2)

Michael Parr
Michael Parr

Reputation: 134

Just to clarify what icemanind said, you want to remove any case issues with your string. I can see that you have Strawberry (capital S) and orange (no capital) which means you haven't set a real string structure. Remember that when you check to see if the strings match, that they have to type it in exactly as you test it.

So you use the .toUpper or .toLower. This will convert the text from the textbox to either lower or uppercase - then the string you test should be in all lower of uppercase (lower being easier i guess.)

So each line should be more like...

If Label1.Text = "1" Andalso TextBox2.Text.ToLower = "banana" Then

Now you can type BAnaNa into the textbox, and it will be tested as banana. Also notice i've used the Andalso - its good for future use. Andalso should be used in these kinds of tests, because it only checks the 2nd condition if the first is valid. And checks both, even if the 1st is false. Not a big thing, but good practice and better performance.

Upvotes: 0

Icemanind
Icemanind

Reputation: 48686

The answer to your question is probably because of a casing issue. Try using ToLower() and make sure your strings are lower case too.

As a side note though, you could write this a lot more efficient if you created an abstract class called Fruit and then derive the different types of fruit (apple, strawberry, etc.) from that class. Then you could just create an abstract method called ToString() and compare the input to the ToString() method. This will save you from having to have a crap ton of "If"'s in your code.

Upvotes: 2

Related Questions