1. Introduction to the Visual Basic Language and Environment
Example 1-3: Stopwatch Application - Attaching Code
All that�s left to do is attach code to the application. We write code
for every event a response is needed for. In this application,
there are three such events: clicking on each of the command buttons.
- Double-click anywhere on the form to open the code window.
Or, select �View Code� from the project window.
- Click the down arrow in the Object box and select the object
named (general). The Procedure box will show (declarations).
Here, you declare three form level variables:
Option Explicit
Dim StartTime As Variant
Dim EndTime As Variant
Dim ElapsedTime As Variant
The Option Explicit statement forces us to declare all variables.
The other lines establish StartTime, EndTime, and
ElapsedTime as variables global within the form.
- Select the cmdStart object in the Object box. If the
procedure that appears is not the Click procedure, choose Click
from the procedure box. Type the following code which begins the
timing procedure. Note the Sub and End Sub statements
are provided for you:
Sub cmdStart_Click ()
�Establish and print starting time
StartTime = Now
lblStart.Caption = Format(StartTime, "hh:mm:ss")
lblEnd.Caption = ""
lblElapsed.Caption = ""
End Sub
In this procedure, once the Start Timing button is clicked,
we read the current time and print it in a label box. We also blank
out the other label boxes. In the code above (and in all code in
these notes), any line beginning with a single quote (�) is a comment.
You decide whether you want to type these lines or not. They are not
needed for proper application operation.
- Now, code the cmdEnd button.
Sub cmdEnd_Click ()
�Find the ending time, compute the elapsed time
�Put both values in label boxes
EndTime = Now
ElapsedTime = EndTime - StartTime
lblEnd.Caption = Format(EndTime, "hh:mm:ss")
lblElapsed.Caption = Format(ElapsedTime, "hh:mm:ss")
End Sub
Here, when the End Timing button is clicked, we read the
current time (End Time), compute the elapsed time, and put
both values in their corresponding label boxes.
- And, finally the cmdExit button.
Sub cmdExit_Click ()
End Sub
This routine simply ends the application once the Exit button
is clicked.
- Did you notice that as you typed in the code, Visual Basic does
automatic syntax checking on what you type (if you made any mistakes,
that is)?
- Run your application by clicking the Run button on the toolbar,
or by pressing . Pretty easy, wasn�t it?
- Save your application - see the Primer on the next page.
Use the Save Project As option under the File menu.
Make sure you save both the form and the project files.
- If you have the time, some other things you may try with the
Stopwatch Application:
- Try changing the form color and the fonts used in the label boxes and command buttons.
- Notice you can press the �End Timing� button before the �Start Timing� button. This shouldn�t be so. Change the application so you can�t do this. And make it such that you can�t press the �Start Timing� until �End Timing� has been pressed. Hint: Look at the command button Enabled property.
- Can you think of how you can continuously display the �End Time� and �Elapsed Time�? This is a little tricky because of the event-driven nature of Visual Basic. Look at the Timer tool. Ask me for help on this one.
Counter Hit
This Homepage is special brought to you by CK Tan