• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

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?
0
jettman26
Asked:
jettman26
  • 9
  • 8
1 Solution
 
S-TwilleyCommented:
   Sub StartTimer(ByVal OffsetMinutes As Integer)
        tmrCountdown.Stop()

        dtStartTime = Now
        dtFinishTime = dtStartTime.AddMinutes(OffsetMinutes)
        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) * 60
            iSecondsLeft = tsTimeLeft.Seconds
            iMilliLeft = Fix(tsTimeLeft.Milliseconds / 10)

            newText = iSecondsLeft.ToString("00") & ":" & iMilliLeft.ToString("00")

            If iMinutesLeft > 0 Then
                newText = iMinutesLeft.ToString("00") & ":" & newText
            End If
            txtCountdown.Text = newText

            If Now.Subtract(dtLastBeep).TotalSeconds >= 15 Then
                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
0
 
S-TwilleyCommented:
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
0
 
jettman26Author Commented:
Sorry but I am rather new at this.  
What do I declare the variables as?  I tried As DateTime but it didn't work.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
S-TwilleyCommented:
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
0
 
jettman26Author Commented:
What about txtCountdown and tmrCountdown?
0
 
S-TwilleyCommented:
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
0
 
jettman26Author Commented:
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?
0
 
S-TwilleyCommented:
ok...

  Sub StartTimer_Seconds(ByVal OffsetSeconds As Integer)
        tmrCountdown.Stop()

        dtStartTime = Now
        dtFinishTime = dtStartTime.AddSeconds(OffsetSeconds)
        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.Subtract(dtPauseTime))
        dtFinishTime = dtFinishTime.Add(Now.Subtract(dtPauseTime))
        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
0
 
S-TwilleyCommented:
   Sub PauseCountdown()
        If tmrCountdown.Enabled = False Then Exit Sub
        tmrCountdown.Stop()
        dtPauseTime = Now
    End Sub
0
 
jettman26Author Commented:
Should dtPauseTime be declared as
Dim dtPauseTime As DateTime?
0
 
S-TwilleyCommented:
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
0
 
jettman26Author Commented:
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(OffsetSeconds)
        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.Subtract(dtPauseTime))
        dtFinishTime = dtFinishTime.Add(Now.Subtract(dtPauseTime))
        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?
0
 
jettman26Author Commented:
I increased the point total since it is getting more complicated.
0
 
S-TwilleyCommented:
Ok, here's just so you understand whats going on:

on your form:
- add a textbox called   txtCountdown
- add a textbox called   txtSeconds      ' you can add a label next to this to make it clear what its for
- add a textbox called   txtMinutes       ' you can add a label next to this to make it clear what its for

- add a button called cmdStart         ... change it's text to Start
- add a button called cmdToggle      ... change it's text to Toggle

===============

Open up the code view and insert all this code:

Dim dtStartTime, dtFinishTime, dtLastBeep, dtPauseTime 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 iBeepGap As Integer = 30 ' change this to make the beeps more/less frequent

    Sub StartTimer_Minutes(ByVal OffsetMinutes As Integer)
        StartTimer_Seconds(OffsetMinutes * 60)
    End Sub

    Sub StartTimer_Seconds(ByVal OffsetSeconds As Integer)
        tmrCountdown.Stop()

        dtStartTime = Now
        dtFinishTime = dtStartTime.AddSeconds(OffsetSeconds)
        dtLastBeep = dtStartTime

        txtCountdown.Text = Fix(OffsetSeconds / 60).ToString("00") & ":" & (OffsetSeconds Mod 60).ToString("00") & ":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) * 60
            iSecondsLeft = tsTimeLeft.Seconds
            iMilliLeft = Fix(tsTimeLeft.Milliseconds / 10)

            newText = iSecondsLeft.ToString("00") & ":" & iMilliLeft.ToString("00")

            If iMinutesLeft > 0 Then
                newText = iMinutesLeft.ToString("00") & ":" & newText
            End If
            txtCountdown.Text = newText

            If Now.Subtract(dtLastBeep).TotalSeconds >= iBeepGap Then
                dtLastBeep = Now
                Beep() : Beep() : Beep()
            End If
        Else
            txtCountdown.Text = "00:00:00"
            tmrCountdown.Stop()
        End If
    End Sub

    Sub PauseCountdown()
        If tmrCountdown.Enabled = False Then Exit Sub
        tmrCountdown.Stop()
        dtPauseTime = Now
    End Sub

    Sub ResumeCountdown()
        If tmrCountdown.Enabled = True Then Exit Sub
        If dtFinishTime = Nothing Then Exit Sub
        dtLastBeep = dtLastBeep.Add(Now.Subtract(dtPauseTime))
        dtFinishTime = dtFinishTime.Add(Now.Subtract(dtPauseTime))
        tmrCountdown.Start()
    End Sub

    Sub ToggleCountdown()
        If tmrCountdown.Enabled Then
            PauseCountdown()
        Else
            ResumeCountdown()
        End If
    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        Dim iCountDownFrom As Integer
        If IsNumeric(txtSeconds.Text) And IsNumeric(txtMinutes.Text) Then
            iCountDownFrom = CInt(txtMinutes.Text) * 60
            iCountDownFrom += CInt(txtSeconds.Text)

            StartTimer_Seconds(iCountDownFrom)
        End If
    End Sub

    Private Sub cmdToggle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdToggle.Click
        ToggleCountdown()
    End Sub

================

To change the beep interval... change the iBeepGap variable... you could add another textbox to set this... but i think that's easy enough for you to do



0
 
jettman26Author Commented:
Thanks a ton!
0
 
jettman26Author Commented:
Is there a way to make it play a .wav file instead of a Beep?
0
 
S-TwilleyCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now