Thursday, May 26, 2011

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

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

A.     Try changing the form color and the fonts used in the label boxes and command buttons.

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

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



Quick Primer on Saving Visual Basic Applications:

When saving Visual Basic applications, you need to be concerned with saving both the forms (.FRM) and modules (.BAS) and the project file (.VBP).  In either case, make sure you are saving in the desired directory.  The current directory is always displayed in the Save window.  Use standard Windows techniques to change the current directory.

There are four Save commands available under the File menu in Visual Basic:

Save [Form Name]            Save the currently selected form or module with the current name.  The selected file is identified in the Project window.
Save [Form Name] As      Like Save File, however you have the option to change the file name
Save Project                       Saves all forms and modules in the current project using their current names and also saves the project file.
Save Project As                 Like Save Project, however you have the option to change file names.  When you choose this option, if you have not saved your forms or modules, you will also be prompted to save those files.  I always use this for new projects.

No comments:

Post a Comment