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();
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With