How to use the WinForms Clock control as countdown clock ?
Clock
Clock control can be used as Countdown Clock. It can be achieved by decreasing the seconds value of CustomTime using its attached method - 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 Unfreeze 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 Unfreeze 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 Unfreeze 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 Unfreeze 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
I hope you enjoyed learning about how to use the WinForms Clock control as countdown clock.
You can refer to our WinForms Clock featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our demo to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!