Hi Friends,
This is a simple tutorial to create a Digital Clock in c#. This is presented in a way so that even beginners can learn without any 'hiccups'. So lets start.
Steps
1. Run Visual Studio.
2. Create a New Project -> Windows Form Project and name it as Digitalclock.
Form1.cs
using System;
using System.Windows.Forms;
namespace Digitalclock
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Digital Clock"; //To set the title
timer1.Start(); //starting the timer
}
private void timer1_Tick(object sender, EventArgs e)
{
//to display the time in the label
label1.Text = DateTime.Now.ToString("hh:mm:ss tt");
}
}
}
See the screenshot
This is a simple tutorial to create a Digital Clock in c#. This is presented in a way so that even beginners can learn without any 'hiccups'. So lets start.
Steps
1. Run Visual Studio.
2. Create a New Project -> Windows Form Project and name it as Digitalclock.
3. You will get a blank form. Now design the form like shown in the below image;
Add a label and a Timer (To add a timer just click on the timer on the Toolbox and
click on the form)
4. Now click on the label and press F4 to get the Properties window. Here you can change
the Font size of the label (if required you can change the Font color, Font etc )
5. Now double click on the timer1, you will be directed to Form1.cs (the coding page).
Here update the codes as shown below:
using System;
using System.Windows.Forms;
namespace Digitalclock
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Digital Clock"; //To set the title
timer1.Start(); //starting the timer
}
private void timer1_Tick(object sender, EventArgs e)
{
//to display the time in the label
label1.Text = DateTime.Now.ToString("hh:mm:ss tt");
}
}
}
See the screenshot
6. Your Digital Clock is ready. Now Rebuild and Run your project. See the output shown
below:
below:
7. If you want to load your application to the center of the screen, click on the form and
press F4 for properties. Now change StartPosition - CenterScreen. You can change the BackColor from properties to change the background color of your form.
Conclusion
Now you have learned to create a Digital Clock in c#. In case of any doubts or errors from this project, feel free to comment it down, I can help you.
See Also
No comments:
Post a Comment