Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

1.

Create a standard C# Windows Forms application:

Resized to 63% (was 804 x 540) - Click image to enlarge

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

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:

Now, let's pass to the code.

1. Declare the main variables:

view source

print?
1 public int seconds; // Seconds.
2 public int minutes; // Minutes.

3 public int hours;   // Hours.

4 public bool paused; // State of the timer [PAUSED/WORKING].

2. The code for the starting button:

view source

print?
01 private void button2_Click(object sender, EventArgs e)

02         {

03             if (paused != true)

04             {

                if ((textBox1.Text != "") && (textBox2.Text != "") &&


05
(textBox3.Text != ""))

06                 {
07                     timer1.Enabled = true;

08                     button1.Enabled = true;
09                     button2.Enabled = false;

10                     button3.Enabled = true;

11                     textBox1.Enabled = false;
12                     textBox2.Enabled = false;
13                     textBox3.Enabled = false;
14                     textBox4.Enabled = false;
15 try

16 {

17                     minutes = System.Convert.ToInt32(textBox2.Text);
18                     seconds = System.Convert.ToInt32(textBox3.Text);
19                     hours = System.Convert.ToInt32(textBox1.Text);

20 }

21                     catch (Exception ex)

22                     {

23                         MessageBox.Show(ex.Message);

24                     }

25                 }

26                 else

27                 {

28                     MessageBox.Show("Incomplete settings!");
29                 }

30             }

31             else

32             {

33                 timer1.Enabled = true;

34                 paused = false;
35                 button2.Enabled = false;

36                 button1.Enabled = true;

37             }

38         }

3. The code for the pausing button:

view source

print?
1 private void button1_Click(object sender, EventArgs e)

2         {

3             // Pause the timer.

4             timer1.Enabled = false;

5             paused = true;

6             button1.Enabled = false;
7             button2.Enabled = true;

8         }

4. The code for the stopping button:

view source

print?
01 private void button3_Click(object sender, EventArgs e)

02         {

03             // Stop the timer.

04             paused = false;

05             timer1.Enabled = false;

06             button1.Enabled = false;
07             button3.Enabled = false;

08             button2.Enabled = true;
09             textBox4.Clear();
10             textBox3.Clear();
11             textBox2.Clear();
12             textBox1.Clear();
13             textBox1.Enabled = true;
14             textBox4.Enabled = true;
15             textBox3.Enabled = true;
16             textBox2.Enabled = true;
17             textBox1.Enabled = true;

18             lblHr.Text = "00";

19             lblMin.Text = "00";
20             lblSec.Text = "00";
21         }

5. The code for the timer:

view source

print?
01 private void timer1_Tick(object sender, EventArgs e)

02         {

03             // Verify if the time didn't pass.

04             if ((minutes == 0) && (hours == 0) && (seconds == 0))

05             {

06                 // If the time is over, clear all settings and fields.


                // Also, show the message, notifying that the time is
07
over.

08                 timer1.Enabled = false;

09                 MessageBox.Show(textBox4.Text);

10                 button1.Enabled = false;

11                 button3.Enabled = false;

12                 button2.Enabled = true;
13                 textBox4.Clear();
14                 textBox3.Clear();

15                 textBox2.Clear();

16                 textBox1.Enabled = true;
17                 textBox4.Enabled = true;
18                 textBox3.Enabled = true;
19                 textBox2.Enabled = true;
20                 textBox1.Enabled = true;

21                 lblHr.Text = "00";

22                 lblMin.Text = "00";
23                 lblSec.Text = "00";

24             }

25             else

26             {

27                 // Else continue counting.

28                 if (seconds < 1)

29                 {

30                     seconds = 59;
31                     if (minutes == 0)

32                     {

33                         minutes = 59;

34                         if (hours != 0)
35                             hours -= 1;

36   

37                     }

38                     else

39                     {

40                         minutes -= 1;
41                     }

42                 }

43                 else

44                     seconds -= 1;
                // Display the current values of hours, minutes and
45
seconds in

46                 // the corresponding fields.

47                 lblHr.Text = hours.ToString();

48                 lblMin.Text = minutes.ToString();
49                 lblSec.Text = seconds.ToString();

50             }

51         }

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

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:

Now, let's pass to the code.

1. Declare the main variables:

view source

print?
1 public int seconds; // Seconds.
2 public int minutes; // Minutes.

3 public int hours;   // Hours.

4 public bool paused; // State of the timer [PAUSED/WORKING].


V

private void button2_Click(object sender, EventArgs e)

02         {

03             if (paused != true)

04             {

                if ((textBox1.Text != "") && (textBox2.Text != "") &&


05
(textBox3.Text != ""))

06                 {
07                     timer1.Enabled = true;

08                     button1.Enabled = true;
09                     button2.Enabled = false;

10                     button3.Enabled = true;

11                     textBox1.Enabled = false;
12                     textBox2.Enabled = false;
13                     textBox3.Enabled = false;
14                     textBox4.Enabled = false;
15 try

16 {

17                     minutes = System.Convert.ToInt32(textBox2.Text);
18                     seconds = System.Convert.ToInt32(textBox3.Text);
19                     hours = System.Convert.ToInt32(textBox1.Text);

20 }

21                     catch (Exception ex)

22                     {

23                         MessageBox.Show(ex.Message);

24                     }

25                 }

26                 else

27                 {

28                     MessageBox.Show("Incomplete settings!");
29                 }

30             }

31             else

32             {

33                 timer1.Enabled = true;

34                 paused = false;
35                 button2.Enabled = false;

36                 button1.Enabled = true;

37             }

38         }

private void button1_Click(object sender, EventArgs e)

2         {

3             // Pause the timer.

4             timer1.Enabled = false;

5             paused = true;

6             button1.Enabled = false;
7             button2.Enabled = true;

8         }

private void button3_Click(object sender, EventArgs e)

02         {

03             // Stop the timer.

04             paused = false;

05             timer1.Enabled = false;

06             button1.Enabled = false;
07             button3.Enabled = false;

08             button2.Enabled = true;

09             textBox4.Clear();
10             textBox3.Clear();
11             textBox2.Clear();
12             textBox1.Clear();
13             textBox1.Enabled = true;
14             textBox4.Enabled = true;
15             textBox3.Enabled = true;
16             textBox2.Enabled = true;
17             textBox1.Enabled = true;

18             lblHr.Text = "00";

19             lblMin.Text = "00";
20             lblSec.Text = "00";
21         }

private void timer1_Tick(object sender, EventArgs e)

02         {

03             // Verify if the time didn't pass.

04             if ((minutes == 0) && (hours == 0) && (seconds == 0))

05             {

06                 // If the time is over, clear all settings and fields.


                // Also, show the message, notifying that the time is
07
over.

08                 timer1.Enabled = false;

09                 MessageBox.Show(textBox4.Text);

10                 button1.Enabled = false;

11                 button3.Enabled = false;

12                 button2.Enabled = true;

13                 textBox4.Clear();
14                 textBox3.Clear();
15                 textBox2.Clear();

16                 textBox1.Enabled = true;
17                 textBox4.Enabled = true;
18                 textBox3.Enabled = true;
19                 textBox2.Enabled = true;
20                 textBox1.Enabled = true;

21                 lblHr.Text = "00";

22                 lblMin.Text = "00";
23                 lblSec.Text = "00";

24             }

25             else

26             {

27                 // Else continue counting.

28                 if (seconds < 1)

29                 {

30                     seconds = 59;
31                     if (minutes == 0)

32                     {

33                         minutes = 59;

34                         if (hours != 0)
35                             hours -= 1;

36   

37                     }

38                     else

39                     {

40                         minutes -= 1;
41                     }
42                 }

43                 else

44                     seconds -= 1;
                // Display the current values of hours, minutes and
45
seconds in

46                 // the corresponding fields.

47                 lblHr.Text = hours.ToString();

48                 lblMin.Text = minutes.ToString();
49                 lblSec.Text = seconds.ToString();

50             }

51         }

You might also like