|
|
Title | Make a countdown timer in Visual Basic 6 |
Description | This example shows how to make a countdown timer in Visual Basic 6. |
Keywords | countdown timer, timer, alarm, Visual Basic 6 |
Categories | Algorithms, Controls |
|
|
If you enter a duration in the format h:mm:ss and click the Go button, the following code parses your text to get hours, minutes, and seconds. It adds those values to the current time, saves the result in m_StopTime, and enables the tmrWait timer.
|
|
Private Sub cmdGo_Click()
Dim fields() As String
Dim hours As Long
Dim minutes As Long
Dim seconds As Long
fields = Split(txtDuration.Text, ":")
hours = fields(0)
minutes = fields(1)
seconds = fields(2)
m_StopTime = Now
m_StopTime = DateAdd("h", hours, m_StopTime)
m_StopTime = DateAdd("n", minutes, m_StopTime)
m_StopTime = DateAdd("s", seconds, m_StopTime)
tmrWait.Enabled = True
End Sub
|
|
When the timer fires, it checks the current time. If the stop time has arrived, the code disables the timer and maximizes the form to get your attention.
If the stop time has not arrived, the code determines how many hours, minutes, and seconds remain and displays those values.
|
|
Private Sub tmrWait_Timer()
Dim time_now As Date
Dim hours As Long
Dim minutes As Long
Dim seconds As Long
time_now = Now
If time_now >= m_StopTime Then
Me.WindowState = vbMaximized
tmrWait.Enabled = False
lblRemaining.Caption = "0:00:00"
Else
seconds = DateDiff("s", time_now, m_StopTime)
minutes = seconds \ 60
seconds = seconds - minutes * 60
hours = minutes \ 60
minutes = minutes - hours * 60
lblRemaining.Caption = _
Format$(hours) & ":" & _
Format$(minutes, "00") & ":" & _
Format$(seconds, "00")
End If
End Sub
|
|
|
|
|
|