Introduction to Timers

Timers are not necessarily only used for something like a stop watch. They can be used to control how often a loop executes or the value of a string of text, integer or a boolean.


Set up

Open Visual Basic on your computer and start a new standard EXE project. Save now if you wish. Name your form frmWatch and set its caption to Basic Stop Watch. When completed, this program will be able to start the watch, stop it, reset it and record a lap time. You will need the following objects on your form: * Command Buttons (4) o cmdStart - Used to start the timer o cmdStop - Used to stop the timer o cmdReset - Used to reset the timer/form o cmdLap - Used to execute the code that will record the current time to a text box * Textboxes (1) o txtLap - Used to store the data from the current lap time * Labels (2) o Label1 - Static label, set caption to \"Current Time\" o lblTime - Used to store the current time elapsed * Timers (1) o tmrNew - Used to time


You can place the timer object anywhere on the form as it will not be visible to your users. You can place the rest of the objects any which way, so long as it makes sense... Now we begin to code. The first thing we want to do is click the tmrNew object, scroll to the interval property in the properties window and set it to 100. This means that the timer will execute every 10[sup]th[/sup] of a second. If you set it to 500, it will execute every half second, 1000 will execute every second, etc etc....Also set the enabled property to false because we dont want the timer going off right when the program is loaded. Now double click the timer to bring up the code window for tmrNew.

We need to declare one variable. This variable is going to be used to store and dynamically change the data it holds every time the timer executes. After declaring your variable, we need to set it equal to the value of lblTime's caption. This may seem useless for the start, but this is how we get our 0 to begin with. Now we need to auto increment the value of our variable by 0.1 (or 1/10[sup]th[/sup]) of a second.

If you used any other interval for the timer, you may need to change the number from 0.1 to whatever number fits your interval. The last thing to do is set the caption of lblTime's caption to the data in the variable, but we are going to want to format it for display purposes. Use the below code if you are lost:

Dim sngClock As Single sngClock = Val(lblTime.Caption) sngClock = sngClock + 0.1 lblTime.Caption = Format(sngClock, \"###.0\")

The next thing on our agenda is to start and stop the clock. The bulk of this project is out of the way, the rest of the actions are all executed by one line of code in each procedure (except Reset, a whole three lines). Double click your cmdStart button and add code to enable the timer (tmrNew.enabled = true). Double click your cmdStop button and add code to disable the timer (tmrNew.enabled = false). Double click your cmdResetButton and add code to clear the values of lblTime and txtLap.

Now add code to disable the timer just in case the user clicks Reset before clicking stop. For the final bit, double click your cmdLap button and add code to add the value of lblTime's caption to a new line in the text box. All the code for the aforementioned procedures can be found here:

Private Sub cmdStart_Click() tmrNew.Enabled = True End Sub Private Sub cmdStop_Click() tmrNew.Enabled = False End Sub Private Sub cmdLap_Click() txtLap.Text = txtLap.Text & lblTime.Caption & vbCrLf End Sub Private Sub cmdReset_Click() tmrNew.Enabled = False lblTime.Caption = 0 txtLap.Text = \"\" End Sub Private Sub Form_Load() tmrNew.Enabled = False End Sub

Run the program and play with it. You're done!