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
 
 
 
 
 
TitleDisplay the number of days, minutes, hours, and seconds until an event in Visual Basic .NET
DescriptionThis example shows how to display the number of days, minutes, hours, and seconds until an event in Visual Basic .NET.
Keywordscountdown, timer, VB.NET, days, minutes, hours, seconds
CategoriesMiscellany, Utilities
 
When the program loads, it prompts the user for the event and the time when it will occur. It displays the event name and enables the form's Timer.
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim event_name As String = InputBox("Event")
    If event_name.Length = 0 Then
        Me.Close()
        Exit Sub
    End If

    Dim event_date As String = InputBox("Date (e.g. " & _
        "12/23/09 17:23:00)")
    If event_date.Length = 0 Then
        Me.Close()
        Exit Sub
    End If

    m_EventDate = CDate(event_date)
    lblEvent.Text = event_name

    Me.Text = event_name & " at " & m_EventDate
    tmrCheckTime.Enabled = True
End Sub
 
When the Timer's Tick event fires, the program calculates the time remaining until the event. If the event time has arrived, the program disables the Timer, maximizes the form, and makes the form topmost. It hides all controls except the one that displays the event name, which it centers. Finally it uses the PlaySound API function to play the file tada.wav.

If the event's time ahs not arrived, the program displays the number of days, hours, minutes, and seconds remaining.

 
Private Sub tmrCheckTime_Tick(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    tmrCheckTime.Tick
    Dim remaining As TimeSpan = m_EventDate.Subtract(Now)
    If remaining.TotalSeconds < 1 Then
        tmrCheckTime.Enabled = False
        Me.WindowState = FormWindowState.Maximized
        Me.TopMost = True

        For Each ctl As Control In Me.Controls
            If ctl Is lblEvent Then
                ctl.Location = New Point( _
                    (Me.ClientSize.Width - ctl.Width) \ 2, _
                    (Me.ClientSize.Height - ctl.Height) \ 2)
            Else
                ctl.Visible = False
            End If
        Next ctl

        Try
            PlaySound("tada.wav", PlayParam.SND_ASYNC)
        Catch ex As Exception
            Beep()
        End Try
    Else
        lblDays.Text = remaining.Days & " days"
        lblHours.Text = remaining.Hours & " hours"
        lblMinutes.Text = remaining.Minutes & " minutes"
        lblSeconds.Text = remaining.Seconds & " seconds"
    End If
End Sub
 
To ensure that the file tada.wav is available, I added that file to the project. I selected the file and, in the Properties window, set its "Build Action" property to Content and its "Copy to Output Directory" property to "Copy if newer." Now when the program compiles, it copies this file into the executable directory if necessary.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated