in a frmMainForm i have lblTime (label)
Is there a way that I can display the running time of the computer by putting the values in a label box (lblTime)?
It should display running seconds, minutes, hours, and PM/AM.
any ideas?
in a frmMainForm i have lblTime (label)
Is there a way that I can display the running time of the computer by putting the values in a label box (lblTime)?
It should display running seconds, minutes, hours, and PM/AM.
any ideas?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Apr 2, 2008, 7:05 AM
I think I'm right in saying that the DateTime.Now property will always give the same time as the system clock. So, on that basis, just precede as before but change the Tick eventhandler to:
private
void timer1_Tick(object sender, EventArgs e){
lblTime.Text =
DateTime.Now.ToString("hh:mm:ss tt");}
If you don't want a single digit hour to have a leading zero, then replace 'hh' with 'h'.
Mervha NifflemhellPosted Apr 1, 2008, 12:23 PM
Good day sir.
Your idea about the time is right.
However, is there a way that the ouput would be just the same with the clock timer in the computer (located in the lower right of the screen)?
I just want to put the time coming from the screen to a label. and so, whenever i'll change the real time from the computer itself, the label would also follow as well.
AlanPosted Mar 31, 2008, 2:26 PM
I assume you're talking here about the time since the computer was last switched on, though I couldn't see the relevance of AM:PM as we're talking about a time interval rather than a point in time here.
Anyway, drag a System.Windows.Forms.Timer control onto your form (timer1 say), set its Interval property to 1000 and its Enbabled property to true.
Now double click on the Timer control in the designer to bring up a skeleton for the Tick eventhandler and add the following code:
private
void timer1_Tick(object sender, EventArgs e){
TimeSpan ts = new TimeSpan(Environment.TickCount * 10000L); Console.WriteLine(ts.TotalHours); int hours = ts.Hours; int mins = ts.Minutes; int seconds = ts.Seconds;lblTime.Text =
String.Format("{0:D2}:{1:D2}:{2:D2}", hours, mins, seconds);}