倒计时 10 秒后如何重新启动计时器?
How can I restart the Timer after 10 seconds counting down?
我需要添加一个 Timer
显示从 10 开始的倒计时。当它达到 0 时,它需要从 10 重新开始。这需要在一个连续的循环中。
这是我目前拥有的:
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;
}
}
现在发生的事情是,当它达到 0 时,它会继续减去,例如。 -1 -2 -3 等
我需要它从 10 点重新开始。
谢谢
为什么不简单地将 timeLeft
设置回十
private void myTimer_Tick(object sender, EventArgs e)
{
label2.Text = timeLeft.ToString();
timeLeft -= 1;
if (timeLeft < 0)
{
timeLeft = 10;
}
}
我需要添加一个 Timer
显示从 10 开始的倒计时。当它达到 0 时,它需要从 10 重新开始。这需要在一个连续的循环中。
这是我目前拥有的:
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;
}
}
现在发生的事情是,当它达到 0 时,它会继续减去,例如。 -1 -2 -3 等 我需要它从 10 点重新开始。
谢谢
为什么不简单地将 timeLeft
设置回十
private void myTimer_Tick(object sender, EventArgs e)
{
label2.Text = timeLeft.ToString();
timeLeft -= 1;
if (timeLeft < 0)
{
timeLeft = 10;
}
}