This is a simple C# program to create a digital clock. Here timer tool used to update ‘seconds‘ part in time. To get a timer, open in tool box => All Windows Forms. From given list, search for ‘Timer’. Click and drag timer to your form.

Program name: Clock
Code in ‘form load’ event: timer1.Start();
— Where timer1 is the name of timer.
Code in ‘timer tick’ event: (By double click on timer icon, you will get space to write timer tick event): label1.Text = (DateTime.Now.Hour + “:” + DateTime.Now.Minute + “:” + DateTime.Now.Second).ToString();
–Where label1 is a label in your form.
Over all code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Clock
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = (DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second).ToString();
}
}
}
Output:

Be First to Comment