我对C#还是很陌生,想在我制作的程序中添加一个倒数计时器。除非我添加i--;
,否则计时器在第一次更改后不会更新,但是它开始会占用2秒钟而不是1秒钟,但是此后它将起作用。我不确定这是怎么回事。
public int i = 100;
private void Timer1_Tick(object sender, EventArgs e)
{
if (i < 1)
{
timer1.Stop();
StopWatch.Text = "00:00:00";
}
else
{
i--; //doesn't work without this line
TimeSpan time = TimeSpan.FromSeconds(i);
time = time.Subtract(TimeSpan.FromSeconds(1));
StopWatch.Text = time.ToString(@"hh\:mm\:ss");
}
}
private void Button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Tick += new EventHandler(Timer1_Tick);
timer1.Start();
}
答案 0 :(得分:0)
当Timer1_Tick
首先打勾时,i
为0,因此如果(i < 1)
语句为true
导致timer1.Stop();