Windows Forms: Digital Clock in C#

By FoxLearn 6/25/2017 8:24:36 PM   5.58K
How to make a Digital Clock in C# using Timer Control

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "DigitalClock" and then click OK

c# digital clockStep 2: Design your form as below

digital clock in c#

You need to add a timer control to your form

Step 3: Add code to handle your form as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DigitalClock
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblStatus.Text = DateTime.Now.ToString("T");
        }

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

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //Update label in multiple thread
            Invoke(new MethodInvoker(delegate ()
            {
                lblStatus.Text = DateTime.Now.ToString("T");
            }));            
        }
    }
}

VIDEO TUTORIALS