Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a countdown timer in Visual Basic 2005
DescriptionThis example shows how to make a countdown timer in Visual Basic 2005.
Keywordscountdown timer, timer, alarm, Visual Basic 2005, VB.NET
CategoriesAlgorithms, VB.NET, Controls
 
If you enter a duration in the format h:mm:ss and click the Go button, the following code converts your text into a TimeSpan and adds it to the current time. It saves the result in m_StopTime and enables the tmrWait timer.
 
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    Dim duration As TimeSpan = TimeSpan.Parse(txtTime.Text)
    m_StopTime = Now.Add(duration)

    tmrWait.Enabled = True
End Sub
 
When the timer fires, it calculates the difference between the current time and the desired stop time and displays the time remaining in the label lblRemaining. Then if the total seconds remaining is zero, the program maximizes its form and makes its form topmost so it appears above all other forms, hopefully attracting your attention.
 
Private Sub tmrWait_Tick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles tmrWait.Tick
    Dim remaining As TimeSpan = m_StopTime.Subtract(Now)
    remaining = New TimeSpan(remaining.Hours, _
        remaining.Minutes, remaining.Seconds)
    If remaining.TotalSeconds < 0 Then remaining = _
        TimeSpan.Zero

    lblRemaining.Text = remaining.ToString

    If remaining.TotalSeconds <= 0 Then
        Me.WindowState = FormWindowState.Maximized
        Me.TopMost = True
        tmrWait.Enabled = False
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated