Displaying Current Time

dgorka

Well-known member
Joined
Dec 27, 2006
Messages
88
Programming Experience
5-10
So I've made a form with a status strip. One of the things listed on my status strip is the current time. Now, thats easy enough to get on there, but what I would like it to do is to update to show the current time (not just the time it was when the form loaded). So say I open my form at 1:00 PM, and its still open when the time changes to 1:01 PM, the status strip will reflect it and change to read 1:01 PM. I thought that I'd have to use a timer for this but I couldn't figure out how that would work. If anyone has any ideas(or solutions), please post them. Thanks
 
Nevermind everyone, I got it working.
I had some typing lying here so here it is, better than just deleting your thread, others may be interested in this too. Perhaps here are some info you also didn't think about?

Add the Timer, set enable and interval as needed (for example 30 seconds = 30000 milliseconds). Doubleclick the Timer component to generated the Tick event handler method, where you write the code to display current time. Example where "ToolStripStatusLabel1" is the name of the status strip label:
VB.NET:
ToolStripStatusLabel1.Text = Date.Now.ToShortTimeString
The short time string used here only displays time in hours and minutes, so updating this every 30 seconds will make it close to minute accurate, not more than 29 seconds off if form startup is 'unlucky'.

You set this text with same code initially for example in form load event, after 30 seconds and each 30 seconds that follows the text is updated with the Timer Tick. If you need the minute to change more accurately you have to decrease the Timer interval, for example to update every 5 seconds.
 
Back
Top