Project
Visual Basic 6: Creating a simple stop watch.
Category
Published
Mar 03 2011 by CodeMonkey
Share
Quick jump
I'm following the bulldog build FaTe is making and I thought I'd share a little basic programming which can be expanded into multiple timers for all sorts like timing glue or other things.

I use Visual Basic 6 which is long since been replaced by more recent versions but I'm a devoted fan, so sue me.

Open Visual Basic 6 and select to create a standard project (No frills as I like too call it)

On the form create:
  1. A timer placed anywhere with an interval set to 100.
  2. A button named CmdControl and give it a caption of "Start"
  3. A 2nd button named CmdClear with caption "Clear"
  4. A Label named T:
    • Right click on the label after naming it
    • Select Copy
    • In the Edit menu select paste (Ctrl+V).
    • When asked if you want to control an array select yes
    • Paste 2 more times so you have 4 in total.
Now you want to have the 4 "T" Labels lined up left too right like:


T(0) T(1) T(2) T(3)



With the 2 command buttons below, this is not a large form but you can expand it and change the fonts to suit whatever style you wish.


On to code!


First double click the command button named "Start", this will take you into the code window inside the sub for our button. Insert the following:



Private Sub CmdControl_Click()
If CmdControl.Caption = "Start" Then
Timer1.Enabled = True
CmdControl.Caption = "Stop"
CmdClear.Enabled = False
Else
Timer1.Enabled = False
CmdControl.Caption = "Start"
CmdClear.Enabled = True
End If
End Sub




What our button simply does is check it's own caption text, if it's Start then it's yet to begin - If it is Stop then it's already running!

Depending on which it will either start or stop our timer and enable/disable the stop button so it can't be pressed multiple times by accident.


Private Sub CmdClear_Click()
T(0) = "00": T(1) = "00": T(2) = "00": T(3) = "00"
End Sub




Our Clear button simply resets the labels back too 0.


Private Sub Timer1_Timer()
T(0) = T(0) + 10 'first add the 10th second

If T(0) > 90 Then 'Check if millisecond has reached a second yet
T(1) = T(1) + 1 'add a second
If T(1) < 10 Then T(1) = "0" & T(1)
T(0) = "00" 'set millisecond back to zero
End If

If T(1) > 59 Then 'check if seconds has reached a minute
T(2) = T(2) + 1 'add a minute
If T(2) < 10 Then T(2) = "0" & T(2)
T(1) = "00" 'set seconds back to zero
End If

If T(2) > 59 Then 'check if minutes has reach an hour
T(3) = T(3) + 1 'add the hour
T(2) = "00" 'reset minutes back to zero
End If
End Sub




Basically our timer checks each label to see if it's exceeded a given limit. In the case of milliseconds we can only reach 1000MS before a second has passed, 60 seconds pass equals 1 minute and so on. As each limit is passed the next bigger time factor value is increased and the smaller reset back to 00 for it's next run.


All that's left to do is hit F5 to run your project and test your timer. You can making multiple counters on the same form or compile the project to a Executable (File > Make *****.exe) and simply run multiple timers!
Other projects you might like