Results 1 to 12 of 12

Thread: [RESOLVED] how to make stopwatch count down

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Resolved [RESOLVED] how to make stopwatch count down

    Hi folks.

    I have a stopwatch app that works great, but it counts up from 00:00:00(which is what I set the initial time to be). I can set the label to start at 00:45:00, but how can I make it count down to 00:00:00?

    Thanks in advance.

    Code:
    Public Class MyStopwatch
        Dim sw As New Stopwatch
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles bttnStartStop.Click
            If bttnStartStop.Text = "Start" Then
                bttnStartStop.Text = "Stop"
                Timer1.Start()
                sw.Start()
                Me.TopMost = False
            ElseIf bttnStartStop.Text = "Stop" Then
                bttnStartStop.Text = "Start"
                Timer1.Stop()
                sw.Stop()
                Me.TopMost = True
            End If
        End Sub
    
        Private Sub bttnReset_Click(sender As System.Object, e As System.EventArgs) Handles bttnReset.Click
            Timer1.Stop()
            sw.Reset()
            Label1.Text = "00:00:00"
            bttnStartStop.Text = "Start"
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Dim ts As TimeSpan = sw.Elapsed
            Label1.Text = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)
            If ts.Minutes = 45 Then
                sw.Reset()
                Timer1.Stop()
                frmBreak.ShowDialog()
                Me.TopMost = True
                bttnStartStop.Text = "Start"
                Label1.Text = "00:00:00"
            End If
        End Sub
    End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to make stopwatch count down


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to make stopwatch count down

    Stopwatches measure elapsed time. Time elapses forward, not backwards, so a Stopwatch will only "count up".

    Think about it though. If N ranges from 0 to 10, what does (10 - N) range through? So, with that in mind, if you want to display to the user a time counting down, how can you use the elapsed time of a Stopwatch that counts up to do it? It's very simple logic.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: how to make stopwatch count down

    Quote Originally Posted by .paul. View Post
    Thanks.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: how to make stopwatch count down

    Quote Originally Posted by jmcilhinney View Post
    Stopwatches measure elapsed time. Time elapses forward, not backwards, so a Stopwatch will only "count up".

    Think about it though. If N ranges from 0 to 10, what does (10 - N) range through? So, with that in mind, if you want to display to the user a time counting down, how can you use the elapsed time of a Stopwatch that counts up to do it? It's very simple logic.
    The logic is simple, but the implementation is not. I want to start at 00:45:00 minutes and minus every second that elapses, until 00:00:00 is reached. How can I represent N as 45 minutes? This count down is to be displalyed in the label. Thanks.

    Code:
    Public Class Form1
        Dim sw As New Stopwatch
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer1.Start()
            sw.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Dim ts As TimeSpan = sw.Elapsed
            Label1.Text = String.Format("{0:00}:{1:45}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)
        End Sub
    End Class

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to make stopwatch count down

    Code:
    Public Class Form1
        Dim sw As New Stopwatch
        Dim tsTotal As New TimeSpan(0, 45, 0)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Start()
            sw.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim ts As TimeSpan = sw.Elapsed
            Dim tsElapsed As TimeSpan = tsTotal.Subtract(ts)
            Label1.Text = String.Format("{0:00}:{1:45}:{2:00}", tsElapsed.Hours, tsElapsed.Minutes, tsElapsed.Seconds)
        End Sub
    
    End Class

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: how to make stopwatch count down

    So you need a timespan that represents the count down value. Then that value - the stopwatch elapsed equals how long remaining. That looks like this, without a check for expiration, which you should be able to figure out.

    Code:
        Dim sw As New Stopwatch
        Dim countdown As TimeSpan
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim ts As TimeSpan = countdown - sw.Elapsed
            Label1.Text = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Button1.Enabled = False
            Timer1.Interval = 100
            Timer1.Start()
            countdown = TimeSpan.FromMinutes(45) 'set the countdown
            sw.Start()
        End Sub
    BTW - You and paul should check the difference in the string formats.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to make stopwatch count down

    yeah it was a poor choice of variable names.
    I tried to edit but the site is unresponsive today, or is it just me?

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to make stopwatch count down

    Quote Originally Posted by dbasnett View Post
    BTW - You and paul should check the difference in the string formats.
    ok. I spotted it now

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: how to make stopwatch count down

    I didn't see it until I ran it and the minutes didn't change . Copy / paste =
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: [RESOLVED] how to make stopwatch count down

    Thanks for all the replies. Works now.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to make stopwatch count down

    Quote Originally Posted by vbforever23 View Post
    The logic is simple, but the implementation is not. I want to start at 00:45:00 minutes and minus every second that elapses, until 00:00:00 is reached. How can I represent N as 45 minutes? This count down is to be displalyed in the label. Thanks.

    Code:
    Public Class Form1
        Dim sw As New Stopwatch
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer1.Start()
            sw.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Dim ts As TimeSpan = sw.Elapsed
            Label1.Text = String.Format("{0:00}:{1:45}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)
        End Sub
    End Class
    Let's say that the Stopwatch had timed 45 minutes. How would it represent it? As a TimeSpan, which is the type of its Elapsed property. That should have been your clue and, if you'd read the documentation for the TimeSpan type then you'd have seen its FromMinutes method. You create a TimeSpan from 45 minutes and then, each time you want to calculate the time remaining, you subtract the Elapsed TimeSpan of the Stopwatch from the TimeSpan representing your total period.

    By the way, if you're targeting .NET 4.0 or later, you don't have to use String.Format to display a TimeSpan. TimeSpan.ToString is overloaded so you can provide a format string. It's similar to DateTime.ToString but not quite the same.
    Code:
    myTimeSpan.ToString("hh\:mm\:ss")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width