time2

 

Make A Timer (Backward)


Make A Timer (Backward)

This timer will count from the time you'll set it to zero.


Preparations

Add 1 Timer Control,1 Label and 1 Command Button to your form.
Set the Timer Interval property to 10.
Set the Timer Enabled property to False.
Press the button to Start/pause the timer.


Form Code

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

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


Sub SetTime(h As Integer, m As Integer, s As Integer, t As Integer)
TotalTenthSeconds = t + s * 10 + m * 600 + h * 36000
' update the label
Label1 = h & ":" & m & ":" & s & ":" & t
End Sub

Private Sub Form_Load()
' set the timer
' Call SetTime(hours, minutes, seconds, tenthseconds)
Call SetTime(0, 1, 12, 4)
End Sub

Private Sub Timer1_Timer()
' we set the timer interval to 10, so every tenth second
' this sub will be executed
' if the timer is zeroed, exit sub
If TotalTenthSeconds <= 0 Then
Beep
Label1 = "0:0:0:0"
Timer1.Enabled = False
Exit Sub
End If
' decrease the total amount of Tenth Seconds.
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