65.9K
CodeProject is changing. Read more.
Home

Simple countdown chronometer

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.20/5 (8 votes)

Jun 11, 2008

CPOL
viewsIcon

68774

downloadIcon

2935

Countdown chronometer i've used to help me when im in a exposition

Introduction

Someone ask me to develop a countdown chronometer to use it as a timing reference for expositions. So i did a simple code.

cronoNFun.jpg

Count Down Chronometer with progress bar

cronoFun.jpg

Using the code

This application has three parts: timer1_Tick, button1_click (Start Button), button2_click (restart button).

timer1_Tick:

label1 has the time of textBox1 converted in date time and extracted hours, minutes and seconds. Activates progress bar increment in progress bar step. When the time has gone plays a sound and pop up a message box to alert us.

 label1.Text = Convert.ToString(hr) + ":" + Convert.ToString(min) +
":" + Convert.ToString(seg);
progressBar1.Increment(progressBar1.Step);
if (seg == 0 && min > 0)
{
 min--;
 seg = 59;
}
else if (seg == 0 && min == 0 && hr > 0)
{
 seg = 59; min = 59;
 hr--;
}
if (min == 0 && hr > 0)
{
 min = 59;
 hr--;
}
if (hr == 0 && min == 0 && seg == 0)
{
 timer1.Stop();
 SystemSounds.Beep.Play();
 MessageBox.Show("Listo! Se acab¢ el Tiempo", "ya!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
seg--;

 

Start button:

This button starts the chronometer setting the time parameters for the timer1 and the progress bar(sets the maximum progressBar property in seconds).

private void button1_Click(object sender, EventArgs e)
{
 try
 {
    timer = Convert.ToDateTime(textBox1.Text);
    hr = timer.Hour;
    min = timer.Minute;
    seg = timer.Second;
    mil = timer.Millisecond;
    timer1.Start();
    textBox1.Enabled = false;
    button1.Enabled = false;
    progressBar1.Step = 1;
    progressBar1.Maximum = (min * 60) + seg + (hr * 60 * 60);
    progressBar1.Minimum = 0;
 }
 catch (Exception ex)
 {
  MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 }
}

Reset button:

Stops timer1 and set label1.text in zero

private void button2_Click(object sender, EventArgs e)
{ 
 label1.Text = "0:00:00";
 button1.Enabled = true;
 textBox1.Enabled = true;
 timer1.Stop();
}

         

Like you can see its so simple. Hope to be useful

Keep a running update of any changes or improvements you've made here.