Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

CountDown timer Class in VB.Net

timer countdown countdown timer

  • Please log in to reply
50 replies to this topic

#1 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 22 July 2009 - 05:10 PM

In a lot different programs I wrote countdown timers, in the end it became very boring and frustrating. I decided to make a countdown timer class. And so I did. I will upload it here so everyone who wants to use, will be able to use it.


The class is made in a "professional way" and includes this:

  • Set the time with:
    • Timespan
    • Seconds
    • Minutes
    • Hours
    • Days
  • Add extra time with:
    • Timespan
    • Seconds
    • Minutes
    • Hours
    • Days
  • Start
  • Pause
  • Reset
  • Get the time left in:
    • Seconds
    • Minutes
    • Hours
    • Days
  • Return the number of:
    • Seconds
    • Minutes
    • Hours
    • Days
  • Event Each Second.
  • Event on times up.


It is possible it will contains some bugs, let me know if you find any.



Do you want to make your own class? Look here to see how.

The class you'll find at the bottom.
/Vswe :)


UPDATED:

  • Removed the ability to use milliseconds, this is for avoiding lagging and delays when the event is raised once a millisecond(bad mistake of me), now the smallest value is seconds.
  • small Event bug fix

Attached Files


Edited by Vswe, 23 July 2009 - 01:24 AM.

  • 3

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 22 July 2009 - 05:16 PM

Very cool! I almost posted the code on ASCIIBin so people wouldn't have to download. :)
Thanks for sharing and nice job linking back to your tutorial.
  • 0

#3 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 22 July 2009 - 05:18 PM

I thought it were better to put it in a .txt file rather then post it inside the code tags since it were pretty long. I'm Glad you liked it :D
  • 0

#4 brettus

brettus

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 29 July 2009 - 08:55 AM

excellent program!!
  • 0

#5 jameslcs

jameslcs

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 13 August 2009 - 12:43 PM

how to i use it as a class in VB? How to turn a .txt file into a class?

oh ready know, open a VB project then add new item-> select class. -> delete text in class then,
Copy n paste the text from the .txt file into the class.
Rename the class to CountDown.vb

Edited by WingedPanther, 13 August 2009 - 01:15 PM.
Double post

  • 0

#6 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 16 August 2009 - 10:10 AM

how to i use it as a class in VB? How to turn a .txt file into a class?

oh ready know, open a VB project then add new item-> select class. -> delete text in class then,
Copy n paste the text from the .txt file into the class.
Rename the class to CountDown.vb


I'm glad you get it working. :) The reason I didn't upload it as a .vb file was that CodeCall doesn't support the .vb extension for files.
  • 0

#7 aiu4840

aiu4840

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 20 August 2009 - 07:10 AM

Love it!
  • 0

#8 zapman2003

zapman2003

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 23 August 2009 - 09:41 PM

I think the best things in life were invented out of boredom as opposed to necessity. Love it!
  • 0

#9 jameslcs

jameslcs

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 24 August 2009 - 09:06 AM

Hi, i kinda new in vb, i try to make it work but i can't get the timer running, may be i don't really understand the class much.

can you provide some explanation how the class work. thanks
  • 0

#10 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 24 August 2009 - 09:21 AM

if you need more help, just tell me.


Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Declares a variable for our countdown timer.
        Dim Count As CountDown

        'Set the time to 3 minutes, this can be done in two ways

        '----way 1----
        Count = New CountDown(0, 3)
        '--------


        '----way 2----
        Count = New CountDown
        Count.SetTime(0, 3)
        '--------

        'If we want to add some more time we can do so, here I add 35 seconds.
        Count.AddTime(35)

        'add a handler to the tick event
        AddHandler Count.Tick, AddressOf Count_Tick

        'add a handler to the times up event
        AddHandler Count.TimesOut, AddressOf Times_up


        'starts the timer
        Count.Start()

        'It's also possible to pause and reset the timer.
        '   Count.Pause()
        '   Count.Reset()
    End Sub


    Private Sub Count_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'since we added the handler, this code will occur once each second

        'get the time left
        Me.Text = sender.Days & " days, " & sender.Hours & " hours, " & sender.Minutes & " minutes and " & sender.Seconds & " seconds left."

        'it's alo possible to use Totalseconds, totalminutes etc. 
        'This will return the total amount of them. 
        'For example if we got the time 1 minute and 30 seconds
        'TotalSeconds will return 90 and Totalminutes will return 1.5.
    End Sub

    Private Sub Times_up(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'when the time us up this will happen

        MessageBox.Show("Wake up!!!")
    End Sub

  • 1

#11 jameslcs

jameslcs

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 02 September 2009 - 02:30 PM

i'm still having trouble connecting the 2 events (TimesOut and Tick event), do u mind to show me how to connect the above 2 events.

Don't really understand what to put in the parameter of:

(ByVal sender As Object, ByVal e As EventArgs) in TimeOut event

and

RaiseEvent Tick(Me, e)

Thanks for your time.
  • 0

#12 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 02 September 2009 - 09:03 PM

You don't have to put anything in the parameter. You've already added the handler for the event so the sub is automatically called when the timer reaches 0.
  • 0





Also tagged with one or more of these keywords: timer, countdown, countdown timer

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download