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

[404 byte] By [JoaoDelingerdeSouza] at [2007-12-28]
# 1
Try something like this:

public partial class Form1 : Form {
private DateTime mTrigger; // When to perform action
private TimeSpan mPauseToGo; // Time left to go when paused
private bool mPaused; // Timer paused

public Form1() {
InitializeComponent();
timer1.Interval = 1000;
timer1.Tick += timer1_Tick;
btnStart.Text = "Start";
}

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";
// Run your code here
//...
}
lblToGo.Text = string.Format("{0}:{1:00}", sp.Minutes, sp.Seconds);
}

private void btnStart_Click(object sender, EventArgs e) {
if (mPaused) {
timer1.Enabled = true;
mPaused = false;
mTrigger = DateTime.Now + mPauseToGo;
btnStart.Text = "Pause";
}
else if (timer1.Enabled) {
timer1.Enabled = false;
btnStart.Text = "Resume";
mPauseToGo = mTrigger - DateTime.Now;
mPaused = true;
}
else {
timer1.Enabled = true;
btnStart.Text = "Pause";
mTrigger = DateTime.Now + new TimeSpan(0, 2, 0);
timer1_Tick(this, EventArgs.Empty);
}
}
}

nobugz at 2007-9-4 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

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 :-)

JoaoDelingerdeSouza at 2007-9-4 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

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.

Sc?tt at 2007-9-4 > top of Msdn Tech,Windows Forms,Windows Forms General...