|
By Kardi Teknomo, PhD.
<VB Tutorial | Contents | Previous | Next >
In this section of VB tutorial, we will make a very simple trivia game. The goal is to put the ball between two text boxes. You may download the code in here. Alternatively, you may perform the following steps:
1. Open new VB Standard Exe project
2. Drag and drop 2 text boxes, and 5 command buttons
3. Name the command buttons: cmdStart, cmdBall, cmdLeft, cmdRight and cmdQuit.
4. Change the Caption property of the command buttons
5. Change the BackColor property of the text boxes with any color you like
6. Arrange the place and resize the text boxes and command button like the figure below

7. Copy and paste the following code in the respective command button:
Private Sub cmdBall_Click()
cmdBall.Top = cmdBall.Top - 200
cmdBall.Left = cmdBall.Left - 300 * Rnd + 600
End Sub
Private Sub cmdLeft_Click()
cmdBall.Left = cmdBall.Left - 200
txtLeft.Text = Val(txtLeft.Text) + 1
End Sub
Private Sub cmdQuit_Click()
End
End Sub
Private Sub cmdRight_Click()
cmdBall.Left = cmdBall.Left + 200
txtRight.Text = Val(txtRight.Text) + 1
End Sub
Private Sub cmdStart_Click()
cmdBall.Left = Rnd * Me.Width
cmdBall.Top = Rnd * Me.Height
End Sub
Now you can run the program (F5). Do you understand how it is work?
Rnd = random number
End = to quit the program
Val = to change text to value (or number)
Notice Top, left, width, height property values
<VB Tutorial | Contents | Previous | Next >
Rate this tutorial or give your comments about this tutorial
This tutorial is copyrighted.
Preferable reference for this tutorial is
Teknomo, Kardi. Visual Basic Tutorial . http:\\people.revoledu.com\kardi\
tutorial\VB
|