how to display a counterdown clock using C#
Hi all,
I have a application which is using the System.Windows.Form.Timer to update some data every 2 minutes. I'd like the user to see the countdown on the form.
If the user stops the timer the countdown display should stop either. If that is not possible it's OK for the clock to be reset again to 2 min.
Any simple samples out there how to do it?
Thank you
Joao
Hans,
The code you sent is simply awsome! simple yet efficient :-) well simple for the gurus ..I am a network guy trying to learn this wonderful C# world.
The only thing is that after the 2 min countdown is over and executed I need to reset the counter again to 2 min and start it all over again automatically. Any way to do this? It would be something like the following:
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan sp = mTrigger - DateTime.Now;
if (sp.Ticks < 0)
{
sp = new TimeSpan();
timer1.Enabled = false;
btnStart.Text = "Start";
// I need to actually run this code every 2 minutes
// with the clock displaying the counting down
}
lblToGo.Text = string.Format("{0}:{1:00}", sp.Minutes, sp.Seconds);
}
Initially I thought about having 2 timers (one to show the clock and one to execute my code every 2 min) but later after trying to understand the code above I think I can still use one Timer with Interval=1000 and compare if Ticks equal 0 then execute the code and start everything again. I am still trying to understand the whole code... I was trying to debug it going line by line but I guess that with the Timer the code does not flow as it does in run-time(?)
C# is a awsome language, I am very excited :-)
Just call the btnStart_click function again.
so:
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan sp = mTrigger - DateTime.Now;
if (sp.Ticks < 0)
{
sp = new TimeSpan();
timer1.Enabled = false;
btnStart.Text = "Start";
// Your code to do
btnStart_click(this, e);
}
lblToGo.Text = string.Format("{0}:{1:00}", sp.Minutes, sp.Seconds);
}
This should reset the time.