Page 1 of 1

Countdown Timer How to create a countdown timer application using C# and WinForms.

#1 PixelCard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 139
  • Joined: 20-June 08

Posted 14 July 2008 - 04:00 AM

*
POPULAR

In this tutorial I will explain how to create a countdown application in C#. For this application I did not declare any additional namespaces.

Special Tutorial Requirements:]
  • C# IDE (Visual Studio 2008 used in this tutorial)
  • .NET Framework 1.0

So, here we go.

1. Create a standard C# Windows Forms application:

Posted Image

2. For the newly created form, set the FormBorderStyle property to FixedToolWindow:

Posted Image

Also, set the form Text property to "Countdown Timer" or whatever you want, so you can easily identify the form.

3. Add the following controls to the form:

  • GroupBox (set its Text property to "Activity");
  • Label (set its Text property to "Set time:");
  • 3 TextBox controls (textBox1 (for hours), textBox2 (for minutes), textBox3 (for seconds));
  • 3 Label controls (set their Text property to "hrs", "min" and "sec" respectively);
  • Label (set it's Text property to "Message:");
  • TextBox (textBox4 - - this will contain the message shown at the end of the countdown process);
  • 3 Button controls (button2 - ">" (start the timer), button1 - "| |" (pause the timer), button3 - "[ ]" (stop the timer));
  • 5 Label controls (lblHr - display the hours, lblMin - display the minutes, lblSec - display the seconds; between these three Label controls add two Label controls with the text ":" on them; also change the font size for these labels to 19 or bigger);
  • Timer (set the Interval property to 1000);

The form should look like this:

Posted Image

Now, let's pass to the code.


1. Declare the main variables:

public int seconds; // Seconds.
public int minutes; // Minutes.
public int hours;   // Hours.
public bool paused; // State of the timer [PAUSED/WORKING].



2. The code for the starting button:

private void button2_Click(object sender, EventArgs e)
        {
            if (paused != true)
            {
                if ((textBox1.Text != "") && (textBox2.Text != "") && (textBox3.Text != ""))
                {
                    timer1.Enabled = true;
                    button1.Enabled = true;
                    button2.Enabled = false;
                    button3.Enabled = true;
                    textBox1.Enabled = false;
                    textBox2.Enabled = false;
                    textBox3.Enabled = false;
                    textBox4.Enabled = false;
try
{
                    minutes = System.Convert.ToInt32(textBox2.Text);
                    seconds = System.Convert.ToInt32(textBox3.Text);
                    hours = System.Convert.ToInt32(textBox1.Text);
}
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("Incomplete settings!");
                }
            }
            else
            {
                timer1.Enabled = true;
                paused = false;
                button2.Enabled = false;
                button1.Enabled = true;
            }
        }



3. The code for the pausing button:

private void button1_Click(object sender, EventArgs e)
        {
            // Pause the timer.
            timer1.Enabled = false;
            paused = true;
            button1.Enabled = false;
            button2.Enabled = true;
        }



4. The code for the stopping button:

private void button3_Click(object sender, EventArgs e)
        {
            // Stop the timer.
            paused = false;
            timer1.Enabled = false;
            button1.Enabled = false;
            button3.Enabled = false;
            button2.Enabled = true;
            textBox4.Clear();
            textBox3.Clear();
            textBox2.Clear();
            textBox1.Clear();
            textBox1.Enabled = true;
            textBox4.Enabled = true;
            textBox3.Enabled = true;
            textBox2.Enabled = true;
            textBox1.Enabled = true;
            lblHr.Text = "00";
            lblMin.Text = "00";
            lblSec.Text = "00";
        }



5. The code for the timer:

private void timer1_Tick(object sender, EventArgs e)
        {
            // Verify if the time didn't pass.
            if ((minutes == 0) && (hours == 0) && (seconds == 0))
            {
                // If the time is over, clear all settings and fields.
                // Also, show the message, notifying that the time is over.
                timer1.Enabled = false;
                MessageBox.Show(textBox4.Text);
                button1.Enabled = false;
                button3.Enabled = false;
                button2.Enabled = true;
                textBox4.Clear();
                textBox3.Clear();
                textBox2.Clear();
                textBox1.Enabled = true;
                textBox4.Enabled = true;
                textBox3.Enabled = true;
                textBox2.Enabled = true;
                textBox1.Enabled = true;
                lblHr.Text = "00";
                lblMin.Text = "00";
                lblSec.Text = "00";
            }
            else
            {
                // Else continue counting.
                if (seconds < 1)
                {
                    seconds = 59;
                    if (minutes == 0)
                    {
                        minutes = 59;
                        if (hours != 0)
                            hours -= 1;

                    }
                    else
                    {
                        minutes -= 1;
                    }
                }
                else
                    seconds -= 1;
                // Display the current values of hours, minutes and seconds in
                // the corresponding fields.
                lblHr.Text = hours.ToString();
                lblMin.Text = minutes.ToString();
                lblSec.Text = seconds.ToString();
            }
        }




Now, the controls are set up and the code is ready. You can now run the application and check its functionality. Note, that the values that the user sets as seconds, minutes and hours must be integers.

Attached File(s)



Is This A Good Question/Topic? 5
  • +

Replies To: Countdown Timer

#2 Fuingurth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Posted 19 August 2009 - 09:36 AM

Do you know how I could make it so that I can have 2 timers doing this?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Edy*


Reputation:

Posted 20 April 2010 - 02:38 AM

Hai, do you happen to know how to count up? When you press your button once, the text inside the label will increase automatically?
Was This Post Helpful? 0

#4 TowerWill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 10

Posted 21 April 2010 - 01:51 PM

View PostPixelCard, on 14 July 2008 - 03:00 AM, said:

In this tutorial I will explain how to create a countdown application in C#. For this application I did not declare any additional namespaces.

Special Tutorial Requirements:]
  • C# IDE (Visual Studio 2008 used in this tutorial)
  • .NET Framework 1.0

So, here we go.

1. Create a standard C# Windows Forms application:

Posted Image

2. For the newly created form, set the FormBorderStyle property to FixedToolWindow:

Posted Image

Also, set the form Text property to "Countdown Timer" or whatever you want, so you can easily identify the form.

3. Add the following controls to the form:

  • GroupBox (set its Text property to "Activity");
  • Label (set its Text property to "Set time:");
  • 3 TextBox controls (textBox1 (for hours), textBox2 (for minutes), textBox3 (for seconds));
  • 3 Label controls (set their Text property to "hrs", "min" and "sec" respectively);
  • Label (set it's Text property to "Message:");
  • TextBox (textBox4 - - this will contain the message shown at the end of the countdown process);
  • 3 Button controls (button2 - ">" (start the timer), button1 - "| |" (pause the timer), button3 - "[ ]" (stop the timer));
  • 5 Label controls (lblHr - display the hours, lblMin - display the minutes, lblSec - display the seconds; between these three Label controls add two Label controls with the text ":" on them; also change the font size for these labels to 19 or bigger);
  • Timer (set the Interval property to 1000);

The form should look like this:

Posted Image

Now, let's pass to the code.


1. Declare the main variables:

public int seconds; // Seconds.
public int minutes; // Minutes.
public int hours;   // Hours.
public bool paused; // State of the timer [PAUSED/WORKING].



2. The code for the starting button:

private void button2_Click(object sender, EventArgs e)
        {
            if (paused != true)
            {
                if ((textBox1.Text != "") && (textBox2.Text != "") && (textBox3.Text != ""))
                {
                    timer1.Enabled = true;
                    button1.Enabled = true;
                    button2.Enabled = false;
                    button3.Enabled = true;
                    textBox1.Enabled = false;
                    textBox2.Enabled = false;
                    textBox3.Enabled = false;
                    textBox4.Enabled = false;
try
{
                    minutes = System.Convert.ToInt32(textBox2.Text);
                    seconds = System.Convert.ToInt32(textBox3.Text);
                    hours = System.Convert.ToInt32(textBox1.Text);
}
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("Incomplete settings!");
                }
            }
            else
            {
                timer1.Enabled = true;
                paused = false;
                button2.Enabled = false;
                button1.Enabled = true;
            }
        }



3. The code for the pausing button:

private void button1_Click(object sender, EventArgs e)
        {
            // Pause the timer.
            timer1.Enabled = false;
            paused = true;
            button1.Enabled = false;
            button2.Enabled = true;
        }



4. The code for the stopping button:

private void button3_Click(object sender, EventArgs e)
        {
            // Stop the timer.
            paused = false;
            timer1.Enabled = false;
            button1.Enabled = false;
            button3.Enabled = false;
            button2.Enabled = true;
            textBox4.Clear();
            textBox3.Clear();
            textBox2.Clear();
            textBox1.Clear();
            textBox1.Enabled = true;
            textBox4.Enabled = true;
            textBox3.Enabled = true;
            textBox2.Enabled = true;
            textBox1.Enabled = true;
            lblHr.Text = "00";
            lblMin.Text = "00";
            lblSec.Text = "00";
        }



5. The code for the timer:

private void timer1_Tick(object sender, EventArgs e)
        {
            // Verify if the time didn't pass.
            if ((minutes == 0) && (hours == 0) && (seconds == 0))
            {
                // If the time is over, clear all settings and fields.
                // Also, show the message, notifying that the time is over.
                timer1.Enabled = false;
                MessageBox.Show(textBox4.Text);
                button1.Enabled = false;
                button3.Enabled = false;
                button2.Enabled = true;
                textBox4.Clear();
                textBox3.Clear();
                textBox2.Clear();
                textBox1.Enabled = true;
                textBox4.Enabled = true;
                textBox3.Enabled = true;
                textBox2.Enabled = true;
                textBox1.Enabled = true;
                lblHr.Text = "00";
                lblMin.Text = "00";
                lblSec.Text = "00";
            }
            else
            {
                // Else continue counting.
                if (seconds < 1)
                {
                    seconds = 59;
                    if (minutes == 0)
                    {
                        minutes = 59;
                        if (hours != 0)
                            hours -= 1;

                    }
                    else
                    {
                        minutes -= 1;
                    }
                }
                else
                    seconds -= 1;
                // Display the current values of hours, minutes and seconds in
                // the corresponding fields.
                lblHr.Text = hours.ToString();
                lblMin.Text = minutes.ToString();
                lblSec.Text = seconds.ToString();
            }
        }




Now, the controls are set up and the code is ready. You can now run the application and check its functionality. Note, that the values that the user sets as seconds, minutes and hours must be integers.

From TowerWill: A very nice project which has helped with my study of C# Windows applications.
Was This Post Helpful? 0
  • +
  • -

#5 andtherus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-May 10

Posted 01 June 2010 - 07:56 PM

Nice little program! I did make a number of adjustments as it had some bugs, now it runs perfect!

1. Updated the "Start" button method to ensure it allows to run the timer when user enters only one integer (ex: seconds only) without being forced to enter all 3 boxes (I slightly changed the variabled but should be self explanatory)

private void startbutton_Click(object sender, EventArgs e)
        {
            if (paused != true) //Timer is not paused (active)
            {

                int.TryParse(textBoxhrs.Text, out hours);
                int.TryParse(textBoxmin.Text, out minutes);
                int.TryParse(textBoxsec.Text, out seconds);

                if (hours >= 1 || (minutes >= 1) || (seconds >= 1))

                //If there is at least one integer entered in any of the 3 boxes, executes; else - //throws an exception
                {
                    timer1.Enabled = true;
                    startbutton.Enabled = true; 
                    pausebutton.Enabled = true; //changed the value to 'true'  
                    stopbutton.Enabled = true; //changed the value to 'true'
                    textBoxhrs.Enabled = false;
                    textBoxmin.Enabled = false;
                    textBoxsec.Enabled = false;
                }
                else
                {
                    MessageBox.Show("Enter at least one integer!");
                }
            }


2. Made the following changes in the "Pause" button method
 private void pausebutton_Click(object sender, EventArgs e) //CODE for the PAUSE button
        {
            // Pause the timer. 
            timer1.Enabled = false; 
            paused = true; //
            startbutton.Enabled = true; // changed to true
            pausebutton.Enabled = false; //changed to false
        }


3. Made the following changes to the "Stop" button
 private void stopbutton_Click (object sender, EventArgs e) //code for the STOP button
        {
            // Stop the timer. 
            paused = false;
            timer1.Enabled = false;
            startbutton.Enabled = true; //changed to true
            stopbutton.Enabled = false; //changed to false
            pausebutton.Enabled = false; //changed to false
            textBoxsec.Clear();
            textBoxmin.Clear();
            textBoxhrs.Clear();
            textBoxhrs.Enabled = true;
            textBoxsec.Enabled = true;
            textBoxmin.Enabled = true;
            textBoxhrs.Enabled = true;
            lblHr.Text = "00";
            lblMin.Text = "00";
            lblSec.Text = "00";
            MessageBox.Show("Timer is Stopped, to re-start press <Start>"); //updated to give user a chance to run the timer again after stoppage.

        }


That's about it, hope it helps. Works for me bug-free!
Was This Post Helpful? 0
  • +
  • -

#6 Guest_criticalmind*


Reputation:

Posted 16 August 2010 - 11:17 PM

can somebody help me how can i make timer with set time to 10mins or more
thats starts on 1 to increasing
Was This Post Helpful? 0

#7 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • View blog
  • Posts: 547
  • Joined: 07-July 10

Posted 17 August 2010 - 08:41 AM

1. Study PixelCard's code.
2. If you want a set time, declare the time in the method instead of reading the time from the boxes.
3. Change subtraction to addition.
4. Profit.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_criticalmind*


Reputation:

Posted 17 August 2010 - 12:34 PM

i just made timer goes to increasing but now how can i declare the time in method
 private void button1_Click(object sender, EventArgs e)
        {
            duration = 60 * 10; //min
            timer1.Start();

        }


i just want to put a 10 min mark on this button and displays a label of 10min and then on the other labels the time counts and not reading input on textboxes but declaring it how can i do that sorry im so noobie.. plz help just my proj.
Was This Post Helpful? 0

#9 Guest_criticalmind*


Reputation:

Posted 18 August 2010 - 03:31 AM

can somebody help i just figure out what to do but just a little more tweaks i just need it to start on 0 and count to 1, 2, 3, and above..
and when it reaches 10 minute mark it will have a message box saying "Times up" plz help...

heres the timer code
private void button1_Click(object sender, EventArgs e)
        {
            // Display the current values of minutes and seconds in
            // the corresponding fields
            // Stop counting.
            label6.Text = minutes1.ToString();
            label7.Text = seconds1.ToString();
         }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Verify if the time didn't pass.
            if ((minutes == 0) && (seconds == 0))
            {
                // If the time  is over, clear  all settings and fields.
                // Also, show the message, notifying that the time is over.
                timer1.Enabled = false;
                lblMin.Text = "00";
                lblSec.Text = "00";
            }
            else
            {
                // Else continue  counting.
                if (seconds < 1)
                {
                    seconds = 59;
                    if (minutes == 0)
                    {
                      
                    }
                    else
                    {
                        minutes += 1;
                    }
                }
                else
                    seconds += 1;
                // Display the current values of hours, minutes and seconds in
                // the corresponding fields
                lblMin.Text = minutes.ToString();
                lblSec.Text = seconds.ToString();
            }
        }


plz help and thnx
Was This Post Helpful? 0

#10 Guest_Sophie*


Reputation:

Posted 13 January 2011 - 07:19 AM

Hello,
Complete beginner here.

Thanks for the tutorial - I have tried building it but unfortunately had no success. The code is free of error messages but when I try to run it, the UI does nothing; clicking the start button produces no effect (ie. no error message in the text box) nor does entering values for the minutes/seconds/hours. (I'm quite sure that all control names etc are correct).

Someone suggested that this was because I was trying to run it on Win7 instead of XP. Would this be correct or is it likey to be another problem?

Thanks in advance.
Was This Post Helpful? 0

#11 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Posted 18 January 2011 - 04:35 AM

Great tutorial, it has helped me greatly. this was written very elegantly :rockon:
Was This Post Helpful? 0
  • +
  • -

#12 911tim  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 11-July 10

Posted 20 January 2012 - 07:07 AM

it's a great tutorial. thank you very much :clap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1