首先看下效果图~
运行前
运行后
然后看下例子的代码
private void timer1_Tick(object sender, EventArgs e)
{
int min = Convert.ToInt32(label1.Text);
int src = Convert.ToInt32(label2.Text);
int far = Convert.ToInt32(label3.Text);
far++;
if (src == 100)
{
min++;
label1.Text = min.ToString();
label2.Text = "0";
if (min == 100)
{
timer1.Enabled = false;
MessageBox.Show("计时器已经达到上限", "提示");
}
}
if (far == 100)
{
src++;
label2.Text = src.ToString();
label3.Text = "0";
far = 0;
}
//far = far % 100;//如果far==101,那就变成1,没有这条的话far会突破100~也可以在上面far==100的时候,让far=0
label3.Text = far.ToString();
}
接着看一下自己写的...
private void timer1_Tick(object sender, EventArgs e)
{
int min = Convert.ToInt32(label1.Text);
int src = Convert.ToInt32(label2.Text);
int far = Convert.ToInt32(label3.Text);
far++;
if (far == 100)
{
src++;
if (src == 60)
{
min++;
src = 0;
label2.Text = src.ToString();
if (min == 100)
{
timer1.Enabled = false;
}
label1.Text = min.ToString();
}
else
{
label2.Text = src.ToString();
}
far = 0;
label3.Text = far.ToString();
}
else
{
far = far % 100;//这个好像可以不用;
label3.Text = far.ToString();
}
}