Timer not getting reset in C#

Viewed 2754

Hi I am getting problem in resetting the Timer control in C# win form application.

I am developing a simple countdown timer.

Sample

When I click on start it starts countdown from 59 seconds. I need the timer should restart from beginning when I click on Start button.

code on timer1_tick

if (hours==0 && minutes==0 && seconds==0)
{
    hours = 0;
    minutes = 1;
    seconds = 0;
}
else
{
    if (seconds < 1)
    {
        seconds = 59;
        if (minutes == 0)
        {
            minutes = 59;
            if (hours != 0)
               hours -= 1;
        }
        else
        {
            minutes -= 1;
        }
    }
    else
        seconds -= 1;

    lblTime.Text = hours + @":" + minutes + @":" + seconds;
}

and code on btnStart_Click

timer1.Enabled = false;
timer1.Enabled = true;

Here I am trying to restart the timer1by enabling and disabling the control but it's not working. I also tried to check with

timer1.Stop();
timer1.Start();

but it starts again from where it was stooped.

How can we resolve this?

5 Answers
Related