Sample Program: Using If....Then....Else


In this program, you have to draw three text boxes, three label boxes, one image box
and four command buttons.Change the of the first text box to x (text box starting from the left), second text box to y and third text box to z.  Change the caption of label1 to "+"and the caption of label2 to "=".Clear the caption of label3 and change its name to display, its purpose is to show that whether the answer is wrong or correct. Now, change the caption of the four command buttons to Start, Next, OK and Stop, you may also change their names following their captions respectively.
You also need to draw a line and insert an image in the image box as shown in my program.
Now let's look at the codes:

Private Sub Stop_Click()

End
End Sub

Private Sub Text1_Change()

End Sub

Sub Start_Click()
' To get any random integers from 0 to 100
Randomize Timer
firstNum = Int(Rnd * 100) + 1
secondNum = Int(Rnd * 100) + 1
x.Text = Str$(firstNum)
y.Text = Str$(secondNum)

End Sub

Private Sub Picture1_Click ( )

End Sub

Private Sub OK_Click( )

If z.Text = x.Text + y.Text  Then
display.Caption = "Correct"
Image2.Visible = "true"
Line1.Visible = "true"
Else
display.Caption = "Wrong"
Image2.Visible = "false"
Line1.Visible = "false"
End If

End Sub

Private Sub Next_Click()
z.Text = ""
display.Caption = ""
Start_Click

End Sub

if you would like the user to press the Enter key after typing the answer, write an event procedure for the keyPress event as follows:

Private Sub z_KeyPress(KeyAscii As Integer)

If (KeyAscii = 13) Then
OK_Click
End If

End Sub

where KeyAscii=13 denotes the Enter key.

After starting  the program, the user (can be a teacher, parent or a child himself) have to click on the Start button. Two numbers will be presented in the first two text box. The user have to key in the answer and click OK. The user will know whether the answer is correct or wrong as it will be shown in the third label. To end, just click on the Stop button.
In this program, you may need to know what is the  Rnd and Int function . Rnd return a random number between 0 and 1 while Int returns the next smallest integer of a number.

Example:   if Rnd=0.7423
                 100*Rnd=74.23
                 Int(100*Rnd)=74

By combining Rnd and Int functions, you can generate random integers of any range.
The Str$ function is to convert a number to text  while Val function is to convert text to number.
 

Please try out the above program. If you have any doubts, don't hesitate to email me.

[Back to contents  page]