Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey everyone here on CodeProject!


So I am working on a small project for my friends & learning purposes.
It's basically a countdown timer & the idea started out simple,
Create 2 timers & add some labels to it for it to show the minutes & seconds left.
I got it to reset at 0 seconds back to 60 so the seconds part is working fine but here is the issue.

When I press the start button it starts the timer but only for a second then I will have to press start again.
& I've tried some if loops but it just didnt want to work.
Would a For loop fix the issue?

What are your thoughts.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

<pre lang="c#">namespace DeadManModeTimer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            //Executes the start of the timers
            timer1.Start();

            //Enables the timers see line 36 & 49
            timer1.Enabled = true;
            timer2.Enabled = true;
        }

        //Holders For Minutes / Seconds
        int m = 30;
        int s = 60;

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            s--;
            label1.Text = s.ToString() + "Seconds ";

            if (s <= 0)
            {
                s = 60;
            }
            if (timer1.Enabled == true)
                {
                s--;
                }

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Enabled = false;

            m--;
            label2.Text = m.ToString() + "Minutes ";

            if (m <= 0)
            {
                m = 30;
            }
        }
    }
}
Posted
Comments
BillWoodruff 12-Nov-15 14:10pm    
If your goal is to show a "countdown:" why use two Timers ?

You don't need any loops.
Just remove the lines of code
timer1.Enabled = false;

and
timer2.Enabled = false;

from your code and the timers will not be disabled.
 
Share this answer
 
Comments
BladeLogan 12-Nov-15 8:00am    
That just made it so as soon as I start the app it starts counting down immediately. I want it to start counting down as soon as I press start
For starters, if isn't a loop - it's a conditional, it's only executed once and can't "loop back" on itself.

Second, you don't enable timer2 at all!

Third, that's some odd code, given that you disable the timer as the first thing you do when it gets it's tick event - which means it won't give a second tick unless your enable it again, which you don't.

Scrap that, are start again.
Instead of having two times, have one, and instead of keeping a minutes and seconds count, use a "target" DateTime value:
C#
private DateTime countdownTarget;
...
   countdownTarget = DateTime.Now.AddSeconds(60 + (30 * 60));
   myTimer.Start();

Then, in your Tick handler check the target:
C#
TimeSpan diff = countdownTarget - DateTime.Now;
int secs = diff.Seconds;
int mins = diff.Minutes;
if (diff.TotalSeconds <= 0)
   {
   countdownTarget = DateTime.Now.AddSeconds(60 + (30 * 60));
   }
You can then use the the two values to set your labels.
 
Share this answer
 
Comments
BladeLogan 12-Nov-15 8:23am    
Didnt work out the way I needed but I will try to use the same logic but in a different way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900