If statement trouble

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
How do you make an if statement where you want two things to happen if one thing is true? Those of you who read my other posts know I'm trying to make a math program. I've posted an example of my code below:

If Me.TextBox1.Text = "" Then
Me.PictureBox1.Visible = True
Else
If Me.TextBox1.Text = Val(Me.MathLabel1.Text) + Val(Me.MathLabel2.Text) Then Me.PictureBox2.Visible = True
Score += 1
If Me.TextBox1.Text <= Val(Me.MathLabel1.Text) + Val(Me.MathLabel2.Text) Or Me.TextBox1.Text >= Val(Me.MathLabel1.Text) + Val(Me.MathLabel2.Text) Then Me.PictureBox1.Visible = True
End If

A quick explanation, the different picture boxes are x's and checmarks to show if you are correct, I want to add 1 point to the variable score when you get it right. If you are wondering why I didn't use an else statement for the wrong answer, I am a noob and was running into trouble with 'else'. It occasionaly gave me an error about needing to be preceeded by an if, which It was.
 
Read here for all the power of the If..Else..Endif statment:
http://msdn2.microsoft.com/en-us/library/752y8abs.aspx

But, briefly, I would think you are looking for something that checks the users answer against your given problem, then displays the image (check or X) and adds to score if appropriate, right? Basically:

VB.NET:
If Me.Textbox1.Text = (Me.Label1.Text + Me.Label2.Text) Then
   Me.PictureBox1.Image = FileNameOfCheckMarkImage
   intVariableScore +=1
Else
   Me.PictureBox1.Image= FileNameOfXMarkImage
End If
 
yeah, but I also need to mark it wrong if the user hasn't entered anything into the field. Also, is there anything wrong with using two different pictureboxes other than the fact that it clutters the form?
So how would the code look with the part needed for the blank fields? would you add that with another else or elseif somehow? I really don't know.
 
Just expand on the code then. A Button1_Click event might look like this:

VB.NET:
If Me.TextBox1.Text = "" Then 
   MessageBox.Show("Invalid Entry")
   Exit Sub
Endif

If Me.Textbox1.Text = (Me.Label1.Text + Me.Label2.Text) Then
   Me.PictureBox1.Image = FileNameOfCheckMarkImage
   intVariableScore +=1
Else
   Me.PictureBox1.Image= FileNameOfXMarkImage
End If

As for two PictureBoxes rather than just two images in one, no not really. Its just (typically) neater and more efficient to have one load the image(s).
 
Last edited:
I'm still applying that code, but i have another quick question. I took your code : If Me.TextBox1.Text = "" Then
MessageBox.Show("Invalid Entry")
Exit Sub
Endif

However It said that end if needed to be preceeded by an if. The only thing I did differently is I grouped the code like this:
If Me.TextBox1.Text = "" Then Me.PictureBox1.Visible = True
If Me.TextBox2.Text = "" Then Me.PictureBox3.Visible = True
If Me.TextBox3.Text = "" Then Me.PictureBox4.Visible = True
If Me.TextBox4.Text = "" Then Me.PictureBox5.Visible = True
If Me.TextBox5.Text = "" Then Me.PictureBox6.Visible = True
If Me.TextBox6.Text = "" Then Me.PictureBox7.Visible = True
Exit Sub
end if

If I ever fully understand if statements I'll die a happy man.
 
Ah, I see now the problem.

VB has a little "tidy" feature that allows you to put an entire IF statement on one like to reduce code size (in lines).

So, when you are doing the above, it interprets each line as its on If..Then..Endif, where the end of the line signifies the Endif.

So, if you do this:

VB.NET:
If 1 = 1 Then MessageBox.Show("True")

That is the same (basically) as typing

VB.NET:
If 1 = 1 Then 
   MessageBox.Show("True")
End If

If you were to put

VB.NET:
If 1 = 1 Then MessageBox.Show("True")
End If

You will get an error because the "End If" on that second line isn't needed.



As for your latest code, I'm not sure what you are doing. The best I can figure is that if your TextBox is empty to WOULDN'T want to show the image, right?

So, the user has 6 questions to answer, then they press "Check Answers" I'm assuming, and you processes the answers and show them right/wrong images. Now, before that you want to check each TextBox for empty and stop the processing, so:

Your Button1_Click event would be more like:

VB.NET:
If (Me.TextBox1.Text = "") OR (Me.TextBox2.Text = "") OR (Me.TextBox1.Text = "") OR (Me.TextBox2.Text = "") OR (Me.TextBox1.Text = "") OR (Me.TextBox2.Text = "") Then
MessageBox.Show("One or more entries is blank.")
Exit Sub
End If

'The above would exit the checking routine if any of their answer boxes are blank when they submit.
'Then you would go on to check each answer for accuracy here.
 
Last edited:
That would work if that was what I wanted. I guess I haven't been very clear
I want the x's to appear when you've left a blank textbox, because you've got the wrong answer. I have two picture boxes beside every textbox ( the user has more than one question to answer), one picturebox for checkmark, one for x. The x should become visible if when the check button is pressed or the time runs out you have an incorrect or blank answer.
 
Ah, a bit more detail now =) Well, hopefully you learned something new from my response anyway regarding the If statement structure.

Sounds like your flowing like this:

1) Load the form showing the questions and answer boxes.
2) Start timer countdown.
3) Timer runs out or user submits answers.
4) Process answers and show appropriate pictures.

Your current problem seems to be around item 4, but luckily it isn't as hard as you might think.

In your check function/event you just need a few nested If statements:
VB.NET:
If Me.Textbox1.Text = "" Then 
   Me.PictureBox1.Image = FileNameOfXMarkImage
Else
   'If the textbox contained text, then we end up here.
   If Me.Textbox1.Text = Me.Label1.Text + Me.Label2.Text Then
      Me.PictureBox1.Image = FileNameOfCheckMarkImage
   Else
      Me.PictureBox1.Image = FileNameofXMarkImage
   End If
End If
Do that 6 times (once for each Textbox/Label/PictureBox set) and your good I think.
 
Lol. I've already done it the way you said before, and it seems to be working. Here is my new code:
if Me.TextBox1.Text = "" Then
Me.PictureBox1.Visible = True

End If
If Me.TextBox2.Text = "" Then
Me.PictureBox3.Visible = True

End If
If Me.TextBox3.Text = "" Then
Me.PictureBox4.Visible = True

End If
If Me.TextBox4.Text = "" Then
Me.PictureBox5.Visible = True

End If
If Me.TextBox5.Text = "" Then
Me.PictureBox6.Visible = True

End If
If Me.TextBox6.Text = "" Then
Me.PictureBox7.Visible = True
Exit Sub
End If

That takes care of the empty text boxes, and the rest works fine now. At least I think it does...do you see any problems?
 
Oh wait, there is a problem with what i did. A very big problem. I'm an idiot. U are Right.

So I changed my Code to resemble yours:
If Me.TextBox1.Text = "" Then
Me.PictureBox1.Visible = True
Else
If Me.TextBox1.Text = Val(Me.MathLabel1.Text) + Val(Me.MathLabel2.Text) Then
Me.PictureBox2.Visible = True
Score += 1
Else
Me.PictureBox1.Visible = True
End If

When I did this it gives me an error with the first else, saying it must end with a matching end if
 
What you need is an ElseIf.....

VB.NET:
If Me.TextBox1.Text = "" Then
Me.PictureBox1.Visible = True
ElseIf Me.TextBox1.Text = Val(Me.MathLabel1.Text) + Val(Me.MathLabel2.Text) Then
Me.PictureBox2.Visible = True
Score += 1
Else
Me.PictureBox1.Visible = True
End If
 
yes your code simply needs an additional end if statement as given in the example by raven. the autoindentation that VB does help when deciding if you have it right btw:-

VB.NET:
If blah then                    ' This is the 1st If
     do this
     do this other thing
Else
     if such an such then    ' This is the 2nd If
           do other thing
     Else 
           do whatever
     End If                       ' This ends the second if
End If                            ' This ends the 1st If

note how the final End If lines up with the initial If
 
Why are you checking for the "" (blank) first?
It appears that you can check for the correct answer and assume that anything else is either incorrect or blank.
Do you do something different if it is left blank?

VB.NET:
If YourAnswer  = CorrectAnswer then
  'do something here
else
  'It is blank or wrong
End If
Complex if statements can also be case/select statements....


VB.NET:
Dim YourAnswer As String = TextBox1.Text
Select Case YourAnswer
    Case CorrectAnswer
        'Statements executed if YourAnswer = CorrectAnswer
    Case ""
        'Statements executed if YourAnswer = ""
    Case Else
        'Statements executed if  no Case value is = YourAnswer
End Select
 
I think I started that actually...I have a similar app but for me I needed that a blank input was different than a wrong input. If juggernot counts blank the exact same as wrong, the your method is a bit neater.
 
Back
Top