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.

To play the demo, 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, then add code to handle the Timer_Elapsed event 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 the CircularProgressBar every second.

VIDEO TUTORIAL