2
Answers

I need help making this Countdown Timer

Ask a question

Hey this is my first time here and I already lover these forums. I am new to C# and I have a problem with this Timer I am making. I found this somewhere else on the internet and gave it a shot but changed a few things. I got it to work with the original download after changing the things i didnt like about it, but now that I am doing it on my own script I cant get it right. I've opened the original file(that i modified) and compared andI can't find a difference between the two.
What is supposed to happen is that the user will enter an amont of time in the textboxes and it will be converted to time. When they hit start they the time will be shown on the corresponding labels. Pause button should pause it and the Stop button will clear the fields and stop the time entirely.However when I run it nothing happens. It doesn't even tell me to put numbers in all of text boxes. I really need your help here. THANKS!
 

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Counter
{
    public partial class Counter1 : Form
    {
        public int seconds;
        public int minutes;
        public int hours;
        public bool paused;
        public Counter1()
        {
            InitializeComponent();
        }
        private void CntrStart_Click(object sender, EventArgs e)
        {
            if (paused != true)
            {
                if ((MinTxtBox.Text != "") && (SecTxtBox.Text != ""))
                {
                    Timer1.Enabled = true;
                    CntrPause.Enabled = true;
                    CntrStart.Enabled = false;
                    CntrStop.Enabled = true;
                    try
                    {
                        minutes = System.Convert.ToInt32(MinTxtBox.Text);
                        seconds = System.Convert.ToInt32(SecTxtBox.Text);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("Please Enter Values in ALL Boxes!");
                }
            }
            else
            {
                Timer1.Enabled = true;
                paused = false;
                CntrStart.Enabled = false;
                CntrPause.Enabled = true;
            }
        }
        private void Timer1_Tick(object sender, EventArgs e)
        {
            //Verifying that the time specified has not passed yet
            if ((minutes == 0) && (hours == 0) && (seconds == 0))
            {
                /*this makes it so that if the time has passed it will clear all the fields
                 and also shows the message notifying that time is done*/
                PrdTimer.Enabled = false;
                MessageBox.Show(SecTxtBox.Text);
                CntrPause.Enabled = false;
                CntrStop.Enabled = false;
                CntrStart.Enabled = true;
                MinTxtBox.Clear();
                SecTxtBox.Clear();
                MinTxtBox.Enabled = true;
                SecTxtBox.Enabled = true;
                MinLbl.Text = "00";
                SecLbl.Text = "00";
            }
            else
            {
                if (seconds < 1)
                {
                    seconds = 59;
                    if (minutes == 0)
                    {
                        minutes = 59;
                        if (hours != 0)
                            hours -= 1;
                    }
                    else
                    {
                        minutes -= 1;
                    }
                }
                else
                    seconds -= 1;
                /*Displays the current values of hours,minutes, and seconds
                in the appropriate fields*/
                MinLbl.Text = minutes.ToString();
                SecLbl.Text = seconds.ToString();
            }
        }
        private void CntrPause_Click(object sender, EventArgs e)
        {
            //This section control pausing the timer
            CntrTimer.Enabled = false;
            paused = true;
            CntrPause.Enabled = false;
            CntrStart.Enabled = true;
        }
        private void CntrStop_Click(object sender, EventArgs e)
        {
            //This section controls stopping the timer completley
            CntrTimer.Enabled = false;
            paused = false;
            CntrPause.Enabled = false;
            CntrStop.Enabled = false;
            CntrStart.Enabled = true;
            MinTxtBox.Clear();
            SecTxtBox.Clear();
            MinTxtBox.Enabled = true;
            SecTxtBox.Enabled = true;
            MinLbl.Text = "00";
            SecLbl.Text = "00";
        }
    }
    }
ohh also the timers interval is set at 1thousand

Answers (2)