Category / Section
How to use the WinForms Clock control as countdown clock?
2 mins read
Clock
Clock control can be used as Countdown Clock. It can be achieved by decrease the seconds value of CustomTime using its attached method of AddSeconds and StopTimer property.
StopTimer: Gets or sets the value to freeze or unfreeze time in the clock. To freeze the clock to display a fixed time, the Boolean property StopTimer can be used.
CustomTime: Used to set the custom time.
The following code sample demonstrates the same.
C#
// variables Timer CountDownTimer = new Timer(); bool isFrozen = false; //Set CountdownTimer interval this.CountDownTimer.Interval = 1000; // To Freeze and Un Freeze the Clock this.clock2.StopTimer = true; //To show the custom time this.clock2.ShowCustomTimeClock = true; //To set the Custom time / Reset the clock this.clock2.CustomTime = new DateTime(); this.clock2.CustomTime = this.clock2.CustomTime.AddSeconds(59); // To Freeze and Un Freeze the Clock this.clock2.StopTimer = true; this.buttonAdv1.Text = "Start CountDown"; //CountDownTimer.Tick event this.CountDownTimer.Tick += new EventHandler(CountDownTimer_Tick); // Countdown Timer void CountDownTimer_Tick(object sender, EventArgs e) { if ((this.clock2.CustomTime.Second - 1) > 0) { //Countdown Clock2 time this.clock2.CustomTime = this.clock2.CustomTime.AddSeconds(-1); } else { this.clock2.CustomTime = new DateTime(); //Timer Stop this.CountDownTimer.Stop(); this.buttonAdv1.Text = "Reset the Clock"; } } // Freeze and Unfreeze behavior private void buttonAdv1_Click(object sender, EventArgs e) { if (!isFrozen) { //Timer start this.CountDownTimer.Start(); this.buttonAdv1.Text = "Stop CountDown"; isFrozen = true; } else { //Timer stop this.CountDownTimer.Stop(); this.buttonAdv1.Text = "Start CountDown"; isFrozen = false; } }
VB
' variables Private CountDownTimer As New Timer() Private isFrozen As Boolean = False 'Set CountdownTimer interval Me.CountDownTimer.Interval = 1000 ' To Freeze and Un Freeze the Clock Me.clock2.StopTimer = True 'To show the custom time Me.clock2.ShowCustomTimeClock = True 'To set the Custom time / Reset the clock Me.clock2.CustomTime = New Date() Me.clock2.CustomTime = Me.clock2.CustomTime.AddSeconds(59) ' To Freeze and Un Freeze the Clock Me.clock2.StopTimer = True Me.buttonAdv1.Text = "Start CountDown" 'CountDownTimer.Tick event AddHandler CountDownTimer.Tick, AddressOf CountDownTimer_Tick ' Countdown Timer Private Sub CountDownTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) If (Me.clock2.CustomTime.Second - 1) > 0 Then 'Countdown Clock time Me.clock2.CustomTime = Me.clock2.CustomTime.AddSeconds(-1) Else Me.clock2.CustomTime = New Date() 'Timer Stop Me.CountDownTimer.Stop() Me.buttonAdv1.Text = "Reset the Clock" End If End Sub ' Freeze and Unfreeze behavior Private Sub buttonAdv1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonAdv1.Click If Not isFrozen Then 'Timer start Me.CountDownTimer.Start() Me.buttonAdv1.Text = "Stop CountDown" isFrozen = True Else 'Timer stop Me.CountDownTimer.Stop() Me.buttonAdv1.Text = "Start CountDown" isFrozen = False End If End Sub
Sample Links:
C#: CountDownClock
VB: CountDownClock