user14568805
user14568805

Reputation:

How can I restart the Timer after 10 seconds counting down?

I need to add a Timer displaying a countdown from 10. When it reaches 0 it then needs to start again from 10. This needs to be in a continuous loop.

This is what I have so far:

InitializeComponent();

        timer1.Interval = 1000;
        timer1.Tick += myTimer_Tick;
        timer1.Start();

private void myTimer_Tick(object sender, EventArgs e)
    {
        label2.Text = timeLeft.ToString();
        timeLeft -= 1;

        if (timeLeft < 0)
        {
            timer1.Tick += myTimer_Tick;
        }
    }

What's happening now is when it reaches 0 it continues in the minus, eg. -1 -2 -3 etc. I need it to start again from 10.

Thanks

Upvotes: 1

Views: 226

Answers (1)

Connor Stoop
Connor Stoop

Reputation: 1632

Why not simply set the timeLeft back to ten

private void myTimer_Tick(object sender, EventArgs e)
{
    label2.Text = timeLeft.ToString();
    timeLeft -= 1;

    if (timeLeft < 0)
    {
        timeLeft = 10;
    }
}

Upvotes: 2

Related Questions