Results 1 to 16 of 16

Thread: Req: Countdown Timer - Easiest way please

  1. #1

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Req: Countdown Timer - Easiest way please

    Can someone point me in the right direction of a countdown timer. I've used the search and most of the code refers to clocks with start and stop buttons. I just want to add a simple timer ticking down, no buttons needed to reset or anything.

    **Okay thanks to MetalInquisitor, I have a counter ticking down, but what if I want the ticker to be real and live, so if I close the application the ticker is still counting down, is this possible?



    Thank you.
    Last edited by jokerfool; Mar 3rd, 2013 at 03:33 PM.

  2. #2
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    Re: Req: Countdown Timer - Easiest way please

    Try something like this (I've never used a Timer before, so there might be much more efficient ways to do this)

    I added a Timer and a Label (for displaying countdown) on the Form. Timer starts at 100 and decreases by 1 each passing second (In the Timer Tick event). I set Timer interval at 1000 millisecond which equals to 1 second:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim counter As Integer = 100
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         Timer1.Interval = 1000
    8.         Timer1.Start()
    9.  
    10.     End Sub
    11.  
    12.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    13.  
    14.         counter -= 1
    15.         Label1.Text = counter.ToString
    16.  
    17.     End Sub
    18.  
    19. End Class
    Last edited by MetalInquisitor; Mar 3rd, 2013 at 03:01 PM.

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Req: Countdown Timer - Easiest way please

    Yup like that except I'd probably use a numerical up/down so that you could do the arithmetic directly.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    18

    Re: Req: Countdown Timer - Easiest way please

    Add a timer. On the timer options enable = true.

    Double click timer.


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Label1.Text -= 1

    End Sub

  5. #5

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Req: Countdown Timer - Easiest way please

    Okay let me try this.

  6. #6

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Req: Countdown Timer - Easiest way please

    Okay that worked nicely MetalInquisitor, thank you. Now for the question. If I wanted the timer set for e.g. lets say 6 hrs, but I wanted it live, so if I close it after 30mins and reopen its still ticking down and hasnt restarted, is this possible?

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Req: Countdown Timer - Easiest way please

    What do you mean by "close it after 30mins"? Close what? the timer or the app?



  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Req: Countdown Timer - Easiest way please

    You can make it do exactly what you stated, but what you can't do is have an app that keeps counting down and will take some action if it is closed. Therefore, you HAVE to have the app running for the action to take place. It might be hidden, but it MUST be running. If you know that it will be running, then you can keep the timer going, though with a bit of difficulty (because the timer wouldn't actually be running, you'd be faking it). What you would do would be to save the amount of time on the timer AND the current system time, into two different project Settings. When the app starts, it checks those settings. If the settings are there, it subtracts the saved time from the current system time to determine how much time has elapsed since the app closed. You would then subtract that elapsed time from the countdown that was saved in the other setting, and if there is time left, you'd start counting down from there.
    My usual boring signature: Nothing

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Req: Countdown Timer - Easiest way please

    Seems a bit long winded. Why not just add the countdown period to Now to get a target time and save that to Settings?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Req: Countdown Timer - Easiest way please

    Yeah, that would work, too, with the same caveats about the program needing to be running. You are saving the same thing either way, my way just used an extra setting for no good reason.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Req: Countdown Timer - Easiest way please

    Okay rephrasing question. I wanted to have a Live Countdown timer, so I understand that some other variables may come into it like running from a domain or something, so if it ticks down from 12 hrs and it gets too 11hrs and 32mins and the user closes the app then reopens the app it might now say 9 hrs 13 mins.

    So the live information would have to come from somewhere else and displayed through the software.

    Possible?

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Req: Countdown Timer - Easiest way please

    I'm curious....have you considered that the user could switch off the computer ? I mean is that important ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Req: Countdown Timer - Easiest way please

    your going to have to have something running in the background.

    look into minimizing your form into the system tray, that way its hidden, then create a menu for the tray icon that you can use to set up the timer
    use a 2nd form for the settings only, dont add a timer control to it.
    that way you can unload that particular form from memory when not in use.

    and then you can use other icon properties to display a time or hide and show the main form which you can design to show the time.

    system tray icon is simple to do (was in vb6 so .net should be easier)

    save the starting time to a file and use that to determine the current time left using the time() function or something in case its turned off.
    Last edited by GBeats; Mar 4th, 2013 at 03:40 AM.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  14. #14
    Lively Member FireDust's Avatar
    Join Date
    Feb 2013
    Location
    Slovakia
    Posts
    112

    Re: Req: Countdown Timer - Easiest way please

    [Ok, guys above me said what I wanted to say]
    By the way, GBeats said exactly what I did
    Last edited by FireDust; Mar 4th, 2013 at 03:45 AM.

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Req: Countdown Timer - Easiest way please

    What dunfiddlin and I suggested would behave exactly the way you describe without having some kind of external source, or anything running in the background. The key question is whether or not the count reaching 0 should do anything when the app is not open. If the count reaching 0 should do something at all times, then the app would have to be running, which also means that the computer would have to be running. If you don't care about that, then what we were suggesting is that when the app shuts down, you stop the countdown (it will stop anyways because the app is no longer running), but you store enough information that you can re-start the countdown correctly when the app re-starts. Then, when you start up the app, it checks that stored information, figures out how much time has elapsed since the shutdown, and resumes the countdown at the point it would have reached had you not shut down the app.
    My usual boring signature: Nothing

  16. #16
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Req: Countdown Timer - Easiest way please

    i only added because nobody really made it clear that you need it to run if you want some sort of response, and hiding it in sys tray is a perfect place to keep it out of the way.

    but yea the question was lacking a few details.

    lol always the case, someone mind is runing away with thoughts and of course they know exactly what they want and sometimes its hard to voice it out , hopefully mr jokerfool will have enough info to carry on
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width