)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to use the WinForms Clock control as countdown clock?

Platform: WinForms |
Control: Clock |
Published Date: June 21, 2018 |
Last Revised Date: November 14, 2019 |
.NET Framework: 4

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

 

 

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon