Answered by:
Running clock

Question
-
Hi All,
I basically want a form to load and have a label that displays the running time as HH:MM:SS and it should be a running clock. The seconds should actually be moving. (not just display the current time)
How to do this?
Thanks,
Amit
alexTuesday, May 3, 2011 11:59 AM
Answers
-
This is a timer which starts from zero, and on (you can add hours and what ever you want to it):
System.Windows.Forms.Timer timer2; TimeSpan ts = new TimeSpan(); public Form1() { InitializeComponent(); label1.Text = ""; //timer2: timer2 = new Timer(); timer2.Tick += new EventHandler(timer2_Tick); timer2.Interval = 1000; timer2.Start(); } private void timer2_Tick(object sender, EventArgs e) { ts = ts.Add(TimeSpan.FromSeconds(1)); label1.Text = String.Format("minutes:{0}, seconds:{1}",ts.Minutes, ts.Seconds); }
MitjaTuesday, May 3, 2011 1:15 PM
All replies
-
Hi,
use a windows timer component, and in the timer event, update the label's text property with the current time (datetime.now).
set the interval to 1000 ms, and if you want to format the date to a particular format, use 'somedate.ToString(somecustomformatstring)'
for custom datetime formatstrings, see http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Regards, NicoTuesday, May 3, 2011 12:06 PM -
Create a new Windows Form Application
Add a label control to it
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Timer clockTimer = new Timer(); clockTimer.Enabled = true; clockTimer.Interval = 1000; clockTimer.Tick += new EventHandler(clockTimer_Tick); } void clockTimer_Tick(object sender, EventArgs e) { this.label1.Text = DateTime.Now.ToString("hh:mm:ss"); } } }
Thanks,
A.m.a.L
[MVP Visual C#]
Dot Net Goodies
Don't hate the hacker, hate the code Tuesday, May 3, 2011 12:07 PM -
There's probably some Whizzo control out there that will do this and a million and one other things besides, but the basics to Do It Yourself are:
A Timer that 'ticks' every second (1000 milliseconds), and
A Label in which to display the formatted time.
private void Timer1_Tick( object sender, EventArgs e ) { this.Label2.Text = DateTime.Now.ToString( "HH:mm:ss" ); }
Regards, Phill W.Tuesday, May 3, 2011 12:15 PM -
No..
I want to start the timer as 00:00:00
DateTime.Now shows me the current time.
Thanks
alexTuesday, May 3, 2011 12:41 PM -
Check it out here:http://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed.aspx
MitjaTuesday, May 3, 2011 12:51 PM -
I am working with Framework 2.0 :(
alexTuesday, May 3, 2011 12:59 PM -
create a form level variable and assign it the current time. In the timer tick, subtract that time from the current time, and use that to assing to the label's text.
Regards, NicoTuesday, May 3, 2011 1:05 PM -
This is a timer which starts from zero, and on (you can add hours and what ever you want to it):
System.Windows.Forms.Timer timer2; TimeSpan ts = new TimeSpan(); public Form1() { InitializeComponent(); label1.Text = ""; //timer2: timer2 = new Timer(); timer2.Tick += new EventHandler(timer2_Tick); timer2.Interval = 1000; timer2.Start(); } private void timer2_Tick(object sender, EventArgs e) { ts = ts.Add(TimeSpan.FromSeconds(1)); label1.Text = String.Format("minutes:{0}, seconds:{1}",ts.Minutes, ts.Seconds); }
MitjaTuesday, May 3, 2011 1:15 PM