Jump to content

CountDown timer Class in VB.Net

- - - - -

  • Please log in to reply
45 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
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.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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.

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
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

#4
brettus

brettus

    Newbie

  • Members
  • Pip
  • 1 posts
excellent program!!

#5
jameslcs

jameslcs

    Newbie

  • Members
  • Pip
  • 7 posts
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


#6
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

jameslcs said:

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.

#7
aiu4840

aiu4840

    Newbie

  • Members
  • Pip
  • 1 posts
Love it!

#8
zapman2003

zapman2003

    Newbie

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

#9
jameslcs

jameslcs

    Newbie

  • Members
  • Pip
  • 7 posts
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

#10
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
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


#11
jameslcs

jameslcs

    Newbie

  • Members
  • Pip
  • 7 posts
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.

#12
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users