I’ll show you how to make a countdown with C# and using the timer control.
To do this, we create a new Windows Forms project and switch to the code view of the form.
For this example, we need some global variables. You could also create it later, but we want to program memory conserving.
Global variables
1 2 3 4 5 6 7 |
string countDownString; Font countDownStringFont; SolidBrush countDownBrush; DateTime endTime; Timer timer; |
countDownString: Contains the current value of the countdown for display.
countDownStringFont: Contains the font to be used for the countdown display.
countDownBrush: Contains the brush (color) of the countdown display.
endTime: Contains the value when the countdown must be at 0.
timer: The timer instance to update the countdown display.
In the next step, the variables are initialized in the Form Load event, the required events are registered and properties are set for correct display. At the end the timer is started.
Form Load Event
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private void Form1_Load(object sender, EventArgs e) { // Initialize timer = new Timer(); countDownStringFont = new Font(new FontFamily("Arial"), 50, FontStyle.Bold); countDownBrush = new SolidBrush(Color.Green); endTime = DateTime.Now.AddSeconds(7200); // Events register timer.Tick += new EventHandler(timer_Tick); this.Paint +=new PaintEventHandler(Form1_Paint); // Set properties and start timer this.DoubleBuffered = true; this.ResizeRedraw = true; timer.Interval = 50; timer.Start(); } |
endTime: We add 7200 seconds at the moment. This gives a countdown value of 2 hours.
DoubleBuffered Property: This is set to true to prevent possible flickering of the form or countdown.
ResizeRedraw Property: This tells the form that if the user changes the shape size, the Form – Paint Event will automatically be called because we want to place the countdown centered on the shape.
Timer Interval Property: Indicates how often the tick event should occur (value 50 for every 50 milliseconds)
Let us now come to the interesting part. The timer tick event.
Timer Tick Event
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
private void timer_Tick(object sender, EventArgs e) { TimeSpan leftTime = endTime.Subtract(DateTime.Now); if (leftTime.TotalSeconds < 0) { countDownString = "00:00:00:00"; countDownBrush.Color = Color.Red; Refresh(); timer.Stop(); MessageBox.Show("Countdown abgelaufen!"); } else { countDownString = leftTime.Hours.ToString("00") + ":" + leftTime.Minutes.ToString("00") + ":" + leftTime.Seconds.ToString("00") + ":" + (leftTime.Milliseconds / 10).ToString("00"); Refresh(); } } |
First, we find out how much time is left by subtracting the time from the end time.
Then we check if the countdown is over or not. When it is over, the countdown string is set to a “null string” and the brush is given the color red to make the ending more visible. Then we call Refresh to force a redraw.
Since the countdown is ready, we can stop the timer.
Finally, we give a message that the countdown is finished.
If the countdown is not over, we’ll add the remaining time to the global countDownString. With the ToString method we make sure that always 2 digits are displayed (eg 08).
Again, we call the refresh method to force redrawing.
When you start the project, not much is happening. It lacks the paint event in which the actual countdown is drawn.
Form Paint Event
1 2 3 4 5 6 7 8 9 10 |
private void Form1_Paint(object sender, PaintEventArgs e) { Size stringSize = Size.Ceiling( e.Graphics.MeasureString(countDownString,countDownStringFont)); e.Graphics.DrawString(countDownString, countDownStringFont, countDownBrush, ((this.Width - 4) / 2) - (stringSize.Width / 2), ((this.Height - 32) / 2) - (stringSize.Height/ 2)); } |
Here we first calculate how big the string in pixels should be drawn. We need this because we want to show the countdown in the middle of the form.
Then the actual countdown is drawn by Graphics.DrawString.Now you can test your personal countdown:
1. Start the countdown
2. Resized From
Alternative Example:
[…] You may also like :C# Create Countdown with Timer […]