Since we're starting at 0, there is no where to count down to, but you appear to have some buttons on your form for adding time to the clock, so let's use those.īecause we'll be updating the time from multiple places, and this also requires a check to see if the timer is disabled and to start it if it isn't, I created a helper method to add time to the clock: private void AddTimeToClock(TimeSpan timeToAdd) So the only thing left to do is enable the timer. (in case it's negative), and stop our timer Countdown clock has run out, so set it to zero If (countdownClock.TotalMilliseconds <= 0) Subtract whatever our interval is from the countdownClockĬountdownClock = countdownClock.Subtract(TimeSpan.FromMilliseconds(timer.Interval)) Since I'm using a different type of timer, the requirements are different: private void OnTimeEvent(object sender, EventArgs e) Notice that the signature of this method is slightly different than yours. LblTime.Text = in answer to one of your main questions: How do we stop the timer when it gets to 0? We can do this in the OnTimeEvent event, since this is the moment when the timer is reading the current countdown clock and removing a second from it. I created one named lblTime: private void DisplayTime() Now that this is done in one place, if you want to change the format, there's only one place to change the code.Īlso, I noticed that you were using a TextBox to display the time, but for a read-only value, it's probably better to use a Label. I wrote a small function to display the time on the form, since we're doing it from more than one place and it has a specific format string. You might be wondering what DisplayTime() is there for. Timer.Interval = (int) TimeSpan.FromSeconds(1).TotalMilliseconds And I used the TimeSpan.FromSeconds method to set the timer interval, which makes it more obvious what the interval is: private void Form1_Load(object sender, EventArgs e) It increases readability and doesn't really slow down coding since Visual Studio gives intellisense as soon as you type t. If anything, we should be subscribing to the event instead, so we have some code to execute when the timer elapses.Īlso notice I renamed your timer from t to timer. Now, in your Form_Load, you're doing the right things, except you are un-subscribing the OnTimeEvent from the timer. If we make this a class-level variable, then we can change it both from the timer's Tick event as well as from buttons that the user clicks: public partial class Form1 : Form We could also use a variable to hold the time that we're displaying. Instead, let's use a : public partial class Form1 : Form They aren't doing anything, and the TimeSpan class already has an easy way to specify a number of minutes or seconds: // We don't need theseĪlso, you are using a, which may cause some troubles when trying to update the control text, since it runs in another thread. Looking at your code from top down, the first thing I would do is get rid of these variables. These methods also set the Enabled property, so if you call Stop, then Enabled will be set to false. It also has a Stop method that you can call when you want it to stop. If this is set to false, the timer is not running.
0 Comments
Leave a Reply. |