Guaranteed Hits to your web site!












Learn Visual Basic 6.0


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.
  1. Double-click anywhere on the form to open the code window. Or, select �View Code� from the project window.

  2. 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.

  3. 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.

  4. 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.

  5. And, finally the cmdExit button.

    Sub cmdExit_Click ()
      End
    End Sub


    This routine simply ends the application once the Exit button is clicked.

  6. 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)?

  7. Run your application by clicking the Run button on the toolbar, or by pressing . Pretty easy, wasn�t it?

  8. 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.

  9. If you have the time, some other things you may try with the Stopwatch Application:


Pen Line


Counter Hit Counter Hit


This Homepage is special brought to you by CK Tan
Hosted by www.Geocities.ws

1