Make A Timer

Make a timer that will display the time (hours, minutes, seconds and tenth seconds) that passed since you start it.

Preparations

Add 1 Timer Control,1 Label and 2 Command Buttons to your form.
Set the Timer Interval property to 10.
Set the Timer Enabled property to False.
Press the first button to start the timer, and the second button to pause/resume it.

Form Code

Dim TotalTenthSeconds, TotalSeconds, TenthSeconds, Seconds, _
Minutes, Hours As Integer

Private Sub Command1_Click()
    ' initilize the Total tenth seconds
    TotalTenthSeconds = -1
    ' start the timer
    Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
    ' start or stop the timer
    Timer1.Enabled = Not Timer1.Enabled
End Sub

Private Sub Timer1_Timer()
    ' increase the total amount of Tenth Seconds.
    ' we set the timer interval to 10, so every tenth second
    ' this sub will be executed

    TotalTenthSeconds = TotalTenthSeconds + 1
    ' if the TotalTenthSeconds is equal to 10, set it to 0.
    TenthSeconds = TotalTenthSeconds Mod 10
    ' 10 tenth seconds are equal to 1 second
    ' int - will give us the integer part of the number:
    ' int(0.9) = 0

    TotalSeconds = Int(TotalTenthSeconds / 10)
    ' if the Seconds is equal to 60, set it to 0
    Seconds = TotalSeconds Mod 60
    Minutes = Int(TotalSeconds / 60) Mod 60
    Hours = Int(TotalSeconds / 3600)
    ' update the label
    Label1 = Hours & ":" & Minutes & ":" & Seconds & ":" & TenthSeconds
End Sub

Go Back