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.
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:
Public Class Form1
Dim counter As Integer = 100
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
counter -= 1
Label1.Text = counter.ToString
End Sub
End Class
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.
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
Re: Req: Countdown Timer - Easiest way please
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?
Re: Req: Countdown Timer - Easiest way please
What do you mean by "close it after 30mins"? Close what? the timer or the app?
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.
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?
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.
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?
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 ?
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.
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 :D :D
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.
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