Windows Forms: Digital Clock in C#

This post shows you How to create a Digital Clock using Circular Progress Bar in C# .NET Windows Forms Application.

Circular ProgressBar

Circular ProgressBar is a custom control for WinForm with animation. It's available as a NuGet package.

First of all, you need to install the CircularProgressBar library, by right-clicking on your project, then select Manage NuGet Packages -> Search CircularProgressBar -> install it.

After installing the CircularProgressBar library, you should rebuild your project, then drag the CircularProgressBar.dll in the packages directory into your visual studio toolbox.

Next, Drag the CircularProgressBar control from the visual studio toolbox to your winform, then design a simple UI as shown below.

digital clock

You should add the timer control to your form to update your digital clock.

Add code to handle the Form_Load event as the following c# code.

private void Form1_Load(object sender, EventArgs e)
{
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 1000;//1s
    timer.Elapsed += Timer_Elapsed;
    timer.Start();
}

Double click on the timer control to add an Elapsed Event handler for the timer control as shown belown.

private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    //Invoke an anonymous method on the thread of the form.
    circularProgressBar1.Invoke((MethodInvoker)delegate
    {
        //Set time to circular progressbar
        circularProgressBar1.Text = DateTime.Now.ToString("hh:mm:ss");
        circularProgressBar1.SubscriptText = DateTime.Now.ToString("tt");//AM or PM
    });
}

As you can see, we will update the text property of CircularProgressBar control every second.

VIDEO TUTORIAL