当我点击启动计时器的按钮时间隔设置为1000毫秒时,它会完成timer1_tick方法中的代码,然后没有任何反应。它永远不会达到其他声明
private void timer1_Tick(object sender, EventArgs e)
{
secondsLeft = 20;
if (secondsLeft > 0)
{
secondsLeft = secondsLeft - 1;
lbSecondsRemaining.Text = secondsLeft.ToString() + " seconds";
}
else
{
timer1.Stop();
MessageBox.Show("Timer stopped");
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
标签lbSecondsRemaining更改为19,然后没有。
答案 0 :(得分:2)
你应该从方法中取出secondsLeft = 20;
,因为每当方法触发时它只会将secondsLeft
设置为20。
private void timer1_Tick(object sender, EventArgs e)
{
if (secondsLeft > 0)
{
secondsLeft = secondsLeft - 1;
lbSecondsRemaining.Text = secondsLeft.ToString() + " seconds";
}
else
{
timer1.Stop();
MessageBox.Show("Timer stopped");
}
}
private void button1_Click(object sender, EventArgs e)
{
secondsLeft = 20;
timer1.Start();
}