Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,860 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,643 people online right now. Registration is fast and FREE... Join Now!




Countdown Timer

 
Reply to this topicStart new topic

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

PixelCard
Group Icon



post 14 Jul, 2008 - 03:00 AM
Post #1


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:

IPB Image

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

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

IPB Image

Now, let's pass to the code.


1. Declare the main variables:

csharp

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:

csharp

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:

csharp

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:

csharp

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:

csharp

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)
Attached File  countdownTimer.zip ( 45.4k ) Number of downloads: 240
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/3/08 07:10PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month