[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
Re: how to make stopwatch count down
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.
Re: how to make stopwatch count down
Quote:
Originally Posted by
.paul.
Thanks.
Re: how to make stopwatch count down
Quote:
Originally Posted by
jmcilhinney
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
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
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.
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?
Re: how to make stopwatch count down
Quote:
Originally Posted by
dbasnett
BTW - You and paul should check the difference in the string formats.
ok. I spotted it now:D
Re: how to make stopwatch count down
I didn't see it until I ran it and the minutes didn't change :eek: . Copy / paste = :confused:
Re: [RESOLVED] how to make stopwatch count down
Thanks for all the replies. Works now. :)
Re: how to make stopwatch count down
Quote:
Originally Posted by
vbforever23
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")