Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Introduction

Here We are going to create an attractive, featured digital clock with Windows
forms and C#. On this clock, we can see the time, date, and day. It will be quite
interesting, so before I start on how to make a digital clock, I want to show you what
we are going to make.

Featured Digital Clock

To create a featured digital clock, we have to follow these steps.

Step 1

First of all, we start Visual Studio. In my case, I have Visual Studio 2022. You can
access that which is installed on your system. After opening the visual studio, click
on Create New Project.

Creating new project


Step 2

Then we select Windows applications with the.NET Framework and press the Next
button.

Choosing the .Net Framework with C#


Step 3
Next, we have to put the name of our application in my case. I have named mine
"featured_digital_Clock." You can put in the name you want and click on "Create."

visual studio
Here we get a default window template and we have to resize our window to
541X238 by dragging. We can define it by code also.

Resizing the form


1
Step 4

And we must use the BackColor property to change the background color to black.

Changing the BackColor


Step 5
Next, we have to add 3 label controls to our form, so to add these label controls, we
have to open the toolbox. To open the toolbox, we have two ways one is, to
go view menu, Under View, we get the toolbox. Another way to open the toolbox is
to press the shortcut key ctrl +Alt+X.
Ctrl+Alt+x is the shortcut key to open the ToolBox. Now we add four labels to our
form by dragging and dropping.

1
Adding the label control
Step 6

Next, we right-click on the Label control and click on properties first we change
their default name lalel1 to lblTime

Changing the ID of the Label


Step 7

Next, we change the default text to 00:00:00 by changing the Text property.

Changing the default text


Step 8
Next, we change the default text color from black to red by changing
the ForeColor property.

Changing the forecolor


Step 9
Next, select all these label controls and open properties and apply the impact.

Changing the default font to impact


Step 10
Next, we add a timer control by opening the toolbox and dragging n drop our form
design

Timer control
Step 11

Next, we need to right-click on the Timer control and open the properties window
and set the interval time 100 to 1000 means in every 1000 milliseconds (each 1
second) tic event that occurs.

Changing the Interval value


Step 12
Next, we repeat the same steps with the other label controls.
label2

name-lblDay [This will show the Day of Week].

Forecolor=White

Text= Monday

Font-size-22

label3
name-lblDate [This will show the date with month and year].

Forecolor=White

Text= 1-1-2023 [any random date]

Font-size-22

Design of Digital clock


Step 13

Next, we just double click on the timer control that will create a tick event of timer
control, and here we assign time to the labels

• Here we have used the inbuilt functions (provided by the C# )Datetime.


Now that returns the current time of your system and converts it to
string format we have used the ToString() method that takes an
argument that Defines the time format if the condition is true then we
passed "hh: mm:ss" this will change the format with 12-hour format
private void timer1_Tick(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
}
Last Step

Now we just need to start timer control when our application loads so just
double click anywhere in the form where no controls are available this will create
the form load event

Here we started our timer with the Start() method


private void Form1_Load(object sender, EventArgs e) {
timer1.Start();
lblDay.Text = DateTime.Now.ToString("dddd");
lblDate.Text = DateTime.Now.ToString("dd-MM-yyyy");
}

So our final output is here.

Digital clock
Summary
• Here we created a featured digital clock with the help of C# and
Windows Form.
• We can choose other color combinations like black-white, yellow-green,
and so on, and we can also choose other fonts based on your interest.
• You can also add a stopwatch feature using the timer.pause() method,
and so on.

You might also like