jettman26
asked on
How to countdown from a specific number
I have a form with a button, a label and a textBox for input.
I want to be able to input a time into the textBox, for example 20 mins.
Then when I click the button I want it to countdown from 20 mins to 0 and show in the label.
I want it to show in this format, 20:00:00
I also want it to beep or play some audio every 30 seconds.
How would I do this?
I want to be able to input a time into the textBox, for example 20 mins.
Then when I click the button I want it to countdown from 20 mins to 0 and show in the label.
I want it to show in this format, 20:00:00
I also want it to beep or play some audio every 30 seconds.
How would I do this?
you start the time using StartTimer(NumberOfMinutes )
the beep is set for every 15 seconds at the moment so change it to what you like
the beep is set for every 15 seconds at the moment so change it to what you like
ASKER
Sorry but I am rather new at this.
What do I declare the variables as? I tried As DateTime but it didn't work.
What do I declare the variables as? I tried As DateTime but it didn't work.
ahh sorry, i meant to paste that part in... apologies:
this is outside the scope of the methods i.e. before the Sub TimerEvent line
Dim dtStartTime, dtFinishTime, dtLastBeep As DateTime
Dim tsTimeLeft As TimeSpan
Dim iSecondsLeft, iMinutesLeft, iMilliLeft As Integer
Dim newText As String
this is outside the scope of the methods i.e. before the Sub TimerEvent line
Dim dtStartTime, dtFinishTime, dtLastBeep As DateTime
Dim tsTimeLeft As TimeSpan
Dim iSecondsLeft, iMinutesLeft, iMilliLeft As Integer
Dim newText As String
ASKER
What about txtCountdown and tmrCountdown?
ahhh, missed another line out of my code... as for txtCountDown, that's just a textbox on your form called txtCountDown
(to put with the other declarations)
Dim WithEvents tmrCountdown As New Timers.Timer(25) 'the 25 is 25 milliseconds which is the update interval
(to put with the other declarations)
Dim WithEvents tmrCountdown As New Timers.Timer(25) 'the 25 is 25 milliseconds which is the update interval
ASKER
Thanks
Is there a way that it could count down from a smaller amount than a minute. Meaning that instead of counting down from 2 minutes it could count down from 30 Seconds or even 2 minutes and 30 seconds?
Also, is there a way to pause the timer and the clicking it again would have it start from where it left off?
Is there a way that it could count down from a smaller amount than a minute. Meaning that instead of counting down from 2 minutes it could count down from 30 Seconds or even 2 minutes and 30 seconds?
Also, is there a way to pause the timer and the clicking it again would have it start from where it left off?
ok...
Sub StartTimer_Seconds(ByVal OffsetSeconds As Integer)
tmrCountdown.Stop()
dtStartTime = Now
dtFinishTime = dtStartTime.AddSeconds(Off setSeconds )
dtLastBeep = dtStartTime
txtCountdown.Text = Fix(OffsetSeconds / 60).ToString("00") & ":" & (OffsetSeconds Mod 60).ToString("00") & ":00"
tmrCountdown.Start()
End Sub
Sub ResumeCountdown()
If tmrCountdown.Enabled = True Then Exit Sub
If dtFinishTime = Nothing Then Exit Sub
dtLastBeep = dtLastBeep.Add(Now.Subtrac t(dtPauseT ime))
dtFinishTime = dtFinishTime.Add(Now.Subtr act(dtPaus eTime))
tmrCountdown.Start()
End Sub
Sub ToggleCountdown()
If tmrCountdown.Enabled Then
PauseCountdown()
Else
ResumeCountdown()
End If
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
ToggleCountdown()
End Sub
Sub StartTimer_Seconds(ByVal OffsetSeconds As Integer)
tmrCountdown.Stop()
dtStartTime = Now
dtFinishTime = dtStartTime.AddSeconds(Off
dtLastBeep = dtStartTime
txtCountdown.Text = Fix(OffsetSeconds / 60).ToString("00") & ":" & (OffsetSeconds Mod 60).ToString("00") & ":00"
tmrCountdown.Start()
End Sub
Sub ResumeCountdown()
If tmrCountdown.Enabled = True Then Exit Sub
If dtFinishTime = Nothing Then Exit Sub
dtLastBeep = dtLastBeep.Add(Now.Subtrac
dtFinishTime = dtFinishTime.Add(Now.Subtr
tmrCountdown.Start()
End Sub
Sub ToggleCountdown()
If tmrCountdown.Enabled Then
PauseCountdown()
Else
ResumeCountdown()
End If
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
ToggleCountdown()
End Sub
Sub PauseCountdown()
If tmrCountdown.Enabled = False Then Exit Sub
tmrCountdown.Stop()
dtPauseTime = Now
End Sub
If tmrCountdown.Enabled = False Then Exit Sub
tmrCountdown.Stop()
dtPauseTime = Now
End Sub
ASKER
Should dtPauseTime be declared as
Dim dtPauseTime As DateTime?
Dim dtPauseTime As DateTime?
yes, sorry... i slipped it in amongst my other DateTime declares and so forgot to repost that part... but I've tried to prefix my variables so it gives you a hint as to what they are... just wish I was this tidy with my code all the time...
first you start using things like:
Dim dtPauseTie as DateTime
and by the end of the project you start doing
Dim blah as string
Dim xx as integer
anyway, hope all works now
first you start using things like:
Dim dtPauseTie as DateTime
and by the end of the project you start doing
Dim blah as string
Dim xx as integer
anyway, hope all works now
ASKER
Thanks for the code.
I think I may be doing something wrong though. I enter the following code and run the app and nothing happens when I push the button.
Dim dtStartTime, dtFinishTime, dtLastBeep As DateTime
Dim tsTimeLeft As TimeSpan
Dim iSecondsLeft, iMinutesLeft, iMilliLeft As Integer
Dim newText As String
Dim WithEvents tmrCountdown As New Timers.Timer(25) 'the 25 is 25 milliseconds which is the update interval
Dim dtPauseTime As DateTime
Sub StartTimer_Seconds(ByVal OffsetSeconds As Integer)
tmrCountdown.Stop()
dtStartTime = Now
dtFinishTime = dtStartTime.AddSeconds(Off setSeconds )
dtLastBeep = dtStartTime
txtCountdown.Text = Fix(OffsetSeconds / 60).ToString("00") & ":" & (OffsetSeconds Mod 60).ToString("00") & ":00"
tmrCountdown.Start()
End Sub
Sub ResumeCountdown()
If tmrCountdown.Enabled = True Then Exit Sub
If dtFinishTime = Nothing Then Exit Sub
dtLastBeep = dtLastBeep.Add(Now.Subtrac t(dtPauseT ime))
dtFinishTime = dtFinishTime.Add(Now.Subtr act(dtPaus eTime))
tmrCountdown.Start()
End Sub
Sub ToggleCountdown()
If tmrCountdown.Enabled Then
PauseCountdown()
Else
ResumeCountdown()
End If
End Sub
Sub PauseCountdown()
If tmrCountdown.Enabled = False Then Exit Sub
tmrCountdown.Stop()
dtPauseTime = Now
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ToggleCountdown()
End Sub
Where do I tell it how many minutes and seconds I want it to countdown?
Also, where do I tell it after how many seconds to Beep?
I think I may be doing something wrong though. I enter the following code and run the app and nothing happens when I push the button.
Dim dtStartTime, dtFinishTime, dtLastBeep As DateTime
Dim tsTimeLeft As TimeSpan
Dim iSecondsLeft, iMinutesLeft, iMilliLeft As Integer
Dim newText As String
Dim WithEvents tmrCountdown As New Timers.Timer(25) 'the 25 is 25 milliseconds which is the update interval
Dim dtPauseTime As DateTime
Sub StartTimer_Seconds(ByVal OffsetSeconds As Integer)
tmrCountdown.Stop()
dtStartTime = Now
dtFinishTime = dtStartTime.AddSeconds(Off
dtLastBeep = dtStartTime
txtCountdown.Text = Fix(OffsetSeconds / 60).ToString("00") & ":" & (OffsetSeconds Mod 60).ToString("00") & ":00"
tmrCountdown.Start()
End Sub
Sub ResumeCountdown()
If tmrCountdown.Enabled = True Then Exit Sub
If dtFinishTime = Nothing Then Exit Sub
dtLastBeep = dtLastBeep.Add(Now.Subtrac
dtFinishTime = dtFinishTime.Add(Now.Subtr
tmrCountdown.Start()
End Sub
Sub ToggleCountdown()
If tmrCountdown.Enabled Then
PauseCountdown()
Else
ResumeCountdown()
End If
End Sub
Sub PauseCountdown()
If tmrCountdown.Enabled = False Then Exit Sub
tmrCountdown.Stop()
dtPauseTime = Now
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ToggleCountdown()
End Sub
Where do I tell it how many minutes and seconds I want it to countdown?
Also, where do I tell it after how many seconds to Beep?
ASKER
I increased the point total since it is getting more complicated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a ton!
ASKER
Is there a way to make it play a .wav file instead of a Beep?
you can, you could either use windows media player control, sound recorder control, or an API (prolly the neatest and less clunky way... but i forget the name of it right now)
but just to alter your code for this... replace this line
Beep() : Beep() : Beep()
with
DoMyBeep()
then add a seperate sub/method
Private Sub DoMyBeep()
' Here will be the code for doing the beep... when we find it
End Sub
----------
This just prevents the methods from becoming too busy
but just to alter your code for this... replace this line
Beep() : Beep() : Beep()
with
DoMyBeep()
then add a seperate sub/method
Private Sub DoMyBeep()
' Here will be the code for doing the beep... when we find it
End Sub
----------
This just prevents the methods from becoming too busy
tmrCountdown.Stop()
dtStartTime = Now
dtFinishTime = dtStartTime.AddMinutes(Off
dtLastBeep = dtStartTime
txtCountdown.Text = OffsetMinutes & ":00"
tmrCountdown.Start()
End Sub
Sub TimerEvent(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs) Handles tmrCountdown.Elapsed
tsTimeLeft = dtFinishTime.Subtract(Now)
If tsTimeLeft.Ticks > 0 Then
iMinutesLeft = tsTimeLeft.Minutes + Fix(tsTimeLeft.TotalHours)
iSecondsLeft = tsTimeLeft.Seconds
iMilliLeft = Fix(tsTimeLeft.Millisecond
newText = iSecondsLeft.ToString("00"
If iMinutesLeft > 0 Then
newText = iMinutesLeft.ToString("00"
End If
txtCountdown.Text = newText
If Now.Subtract(dtLastBeep).T
dtLastBeep = Now
Beep() : Beep() : Beep()
End If
Else
txtCountdown.Text = "00:00:00"
tmrCountdown.Stop()
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
StartTimer(2)
End Sub