?
Solved

How to countdown from a specific number

Posted on 2005-04-05
17
Medium Priority
?
311 Views
Last Modified: 2008-02-26
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
Comment
Question by:jettman26
  • 9
  • 8
17 Comments
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13711783
   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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13711792
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
 

Author Comment

by:jettman26
ID: 13711905
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Expert Comment

by:S-Twilley
ID: 13711937
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
 

Author Comment

by:jettman26
ID: 13711981
What about txtCountdown and tmrCountdown?
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13712011
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
 

Author Comment

by:jettman26
ID: 13712099
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13712203
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13712219
   Sub PauseCountdown()
        If tmrCountdown.Enabled = False Then Exit Sub
        tmrCountdown.Stop()
        dtPauseTime = Now
    End Sub
0
 

Author Comment

by:jettman26
ID: 13713082
Should dtPauseTime be declared as
Dim dtPauseTime As DateTime?
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13713098
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
 

Author Comment

by:jettman26
ID: 13713209
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
 

Author Comment

by:jettman26
ID: 13713214
I increased the point total since it is getting more complicated.
0
 
LVL 12

Accepted Solution

by:
S-Twilley earned 1000 total points
ID: 13713271
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
 

Author Comment

by:jettman26
ID: 13713436
Thanks a ton!
0
 

Author Comment

by:jettman26
ID: 13719085
Is there a way to make it play a .wav file instead of a Beep?
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13719722
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

864 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