Solved

Count down timer

Posted on 2008-10-02
5
621 Views
Last Modified: 2010-04-21
I'm tring to create a timer that would begin counting down from 15 seconds as soon a my form loads. The counter is being displayed in a label. Once the counter reaches 0, a message box would be displayed stating the time is up. I know there has to be a simple way of doing this, but I can't seem to make it work.
~TIA
0
Comment
Question by:98fatboyrider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22627991
What version VB.Net?...
0
 
LVL 11

Accepted Solution

by:
ladarling earned 500 total points
ID: 22628025
Here ya go, you need a timer on your form named myTimer, and a label named Label1
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = 15
        myTimer.Interval = 1000 '1 Second
        myTimer.Enabled = True
    End Sub
    Private Sub HandleTimer(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTimer.Tick
        Dim cVal As Integer = CInt(Label1.Text) - 1
        Label1.Text = cVal
        If cVal = 0 Then
            myTimer.Enabled = False
            MsgBox("Hello world")
        End If
    End Sub

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22628053
put a timer component in your form at design time, set the tick time to 1000 miliseconds.
Create a field at your form class as:
      Private due as DateTime;

Then at your form's Load event start the timer as:
     due = DateTime.Now.AddSeconds(15)
     timer1.Start()

finaly, at your tick event:
     Dim lapse as TimeSpan = due - DateTime.Now;
     Dim sec as Integer = Convert.ToInt32(lapse.TotalSeconds)
     If sec >= 0 Then
         textbox1.Text = sec.ToString()
     Else
         textbox1.Text = "Time Up"
     End If
0
 

Author Closing Comment

by:98fatboyrider
ID: 31502534
Thank you!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22628427
For the halibut...
Public Class Form1
 
    Private WithEvents CD As New CountDown(15)
    Private Delegate Sub TickDelegate(ByVal sender As CountDown, ByVal TimeLeft As Integer)
    Private Delegate Sub CountDownDelegate(ByVal sender As CountDown)
 
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        btnStart.PerformClick()
    End Sub
 
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        CD.Start()
    End Sub
 
    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        CD.Reset()
    End Sub
 
    Private Sub btnAbort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbort.Click
        CD.Abort()
    End Sub
 
    Private Sub CD_Tick(ByVal sender As CountDown, ByVal TimeLeft As Integer) Handles CD.Tick
        If Label1.InvokeRequired Then
            Label1.Invoke(New TickDelegate(AddressOf Me.CD_Tick), New Object() {sender, TimeLeft})
        Else
            Label1.Text = TimeLeft
        End If
    End Sub
 
    Private Sub CD_Aborted(ByVal sender As CountDown) Handles CD.Aborted
        If Me.InvokeRequired Then
            Me.Invoke(New CountDownDelegate(AddressOf Me.CD_Aborted), New Object() {sender})
        Else
            MessageBox.Show("Aborted!")
        End If
    End Sub
 
    Private Sub CD_TimesUp(ByVal sender As CountDown) Handles CD.TimesUp
        If Me.InvokeRequired Then
            Me.Invoke(New CountDownDelegate(AddressOf Me.CD_TimesUp), New Object() {sender})
        Else
            MessageBox.Show("Time's Up!")
        End If
    End Sub
 
    Private Class CountDown
 
        Public Event Tick(ByVal sender As CountDown, ByVal TimeLeft As Integer)
        Public Event Aborted(ByVal sender As CountDown)
        Public Event TimesUp(ByVal sender As CountDown)
 
        Private CountDownTime As Integer = 0
        Private TargetTime As DateTime = DateTime.MinValue
        Private CountAborted As Boolean = False
        Private T As System.Threading.Thread = Nothing
 
        Public Sub New(ByVal TimeInSeconds As Integer)
            Me.CountDownTime = TimeInSeconds
        End Sub
 
        Public Sub Reset()
            Me.TargetTime = DateTime.Now.AddSeconds(Me.CountDownTime)
            Me.CountAborted = False
        End Sub
 
        Public Sub Start()
            If IsNothing(T) Then
                T = New System.Threading.Thread(AddressOf Me.Worker)
                Me.Reset()
                T.Start()
            End If
        End Sub
 
        Public Sub Abort()
            If Not IsNothing(T) andalso  T.IsAlive Then
                Me.CountAborted = True
            End If
        End Sub
 
        Private Sub Worker()
            Dim timeLeft As Integer
            Do
                timeLeft = TargetTime.Subtract(DateTime.Now).Seconds
                RaiseEvent Tick(Me, timeLeft)
                System.Threading.Thread.Sleep(1000)
            Loop While timeLeft > 0 AndAlso Not Me.CountAborted
            T = Nothing
            If Me.CountAborted Then
                RaiseEvent Aborted(Me)
            Else
                RaiseEvent TimesUp(Me)
            End If
        End Sub
 
    End Class
 
End Class

Open in new window

0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question