How do I create a label thats display the time like a digital clock ?
-
My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?
-
My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?
You'll need a thread. I suggest using a
System.Timers.Timer
. -
My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?
There are digital like text/label controls out there. I suggest you do a basic search on such thing.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?
Hi Lim, If you create a timer that ticks every 1 second, and use the code below in the tick event method:
winFormTimerLabel.Text = System.DateTime.UtcNow.TimeOfDay.Hours.ToString() + " : " + System.DateTime.UtcNow.TimeOfDay.Minutes.ToString() + " : " + System.DateTime.UtcNow.TimeOfDay.Seconds.ToString());
I'd imagine it will do what you want. Cheers,Mark Brock Click here to view my blog
-
Hi Lim, If you create a timer that ticks every 1 second, and use the code below in the tick event method:
winFormTimerLabel.Text = System.DateTime.UtcNow.TimeOfDay.Hours.ToString() + " : " + System.DateTime.UtcNow.TimeOfDay.Minutes.ToString() + " : " + System.DateTime.UtcNow.TimeOfDay.Seconds.ToString());
I'd imagine it will do what you want. Cheers,Mark Brock Click here to view my blog
System.DateTime.Now.ToString ( "HH:mm:ss" )
-
My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?
Thanks for your efforts guys. In the end this is what I did. private Timer Clock = new Timer();//create a timer void Clock_Tick(object sender, EventArgs e) { //refresh the time every 1 second this.winFormTimerLabel.Text = DateTime.Now.ToLongTimeString(); } private void Form1_Load(object sender, EventArgs e) { Clock.Interval = 1000;//every 1 second do something //what the program is supposed to do when 1 second elasped Clock.Tick += new EventHandler(Clock_Tick); Clock.Start();//starts the timer } I finally understood where did I went wrong - I did not put in the interval value and I did not start the timer..... silly me :laugh:
-
System.DateTime.Now.ToString ( "HH:mm:ss" )
Show off :).
Mark Brock Click here to view my blog
-
Show off :).
Mark Brock Click here to view my blog
Ha ha! Ha ha! Mine's smaller! ... Oh, wait... :~
-
Thanks for your efforts guys. In the end this is what I did. private Timer Clock = new Timer();//create a timer void Clock_Tick(object sender, EventArgs e) { //refresh the time every 1 second this.winFormTimerLabel.Text = DateTime.Now.ToLongTimeString(); } private void Form1_Load(object sender, EventArgs e) { Clock.Interval = 1000;//every 1 second do something //what the program is supposed to do when 1 second elasped Clock.Tick += new EventHandler(Clock_Tick); Clock.Start();//starts the timer } I finally understood where did I went wrong - I did not put in the interval value and I did not start the timer..... silly me :laugh:
Yup, there you go.
-
My code : private void Form1_Load(object sender, EventArgs e) { winFormTimerLabel.Text = System.DateTime.Now.ToLongTimeString(); } This causes the label to display the current time when the form loads. However, the time does not change. What should I do in order to make the label display the time like a digital clock (with time running)?
keep the code above, just add a timer object!!! edit event tick, and interval propriety. This will trigger your above code all the time!!! The timer will be refreshed. :-D