Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating a running clock in my program C# [duplicate]

I am currently working on a windows form application, and I want a running clock showing the current time and date. The code I have now does make the time and date appear, but it will not update on it's own. Is there a way to make the time and date update on its own?

This is what I currently have:

    lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt"); 
    lblDate.Text = DateTime.Now.ToShortDateString();
like image 913
CodeNComedy Avatar asked Aug 31 '25 17:08

CodeNComedy


1 Answers

You can try like this using the System.Windows.Forms.Timer:

Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in Windows Forms applications and must be used in a window.

System.Windows.Forms.Timer t = null;
private void StartTimer()
{
    t = new System.Windows.Forms.Timer();
    t.Interval = 1000;
    t.Tick += new EventHandler(t_Tick);
    t.Enabled = true;
}

void t_Tick(object sender, EventArgs e)
{
    lblTime.Text = DateTime.Now.ToString();
}
like image 180
Rahul Tripathi Avatar answered Sep 02 '25 19:09

Rahul Tripathi