简体   繁体   中英

A Date-time Label to be updated continuously

lblDTIndicator.Text = DateTime.Now.ToString("MM-dd-yyyy h:mmtt");

The label I have on the form is stuck on the current time for example, I run the program at 10:51 and even though it's 10:55 it doesn't update. How would I have this be updated to be on par with the current latest time. I've tried looking online but haven't found one. I probably am not specific on my search so forgive me, if this seems like a trivial thing at hand. This is using a WinForm

You can use a Timer control and use it's Tick event to update the label.

Simply drag a Timer control to your form, set it's interval to 1 second or whatever update frequency you want. Set it's enabled property to true. Double click the timer control and paste this code in the Tick event:

private void timer1_Tick(object sender, EventArgs e)
{
    lblDTIndicator.Text = DateTime.Now.ToString("MM-dd-yyyy h:mmtt");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM