1)窗体
图中窗体的名称为Form2。换成其他名称,如Form1也可以
2)后台代码
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private int Seconds;
public Form2()
{
InitializeComponent();
// 定时器
timer1.Interval = 1000;
timer1.Enabled = false;
// 定时间隔:25分钟
Seconds = 25 * 60;
ShowTime();
}
private void button1_Click(object sender, EventArgs e)
{
// 启动倒计时
timer1.Start();
}
private void ShowTime()
{
// 显示剩余时间
label1.Text = string.Format("{0:d2}:{1:d2}",
Seconds / 60,
Seconds % 60);
}
private void timer1_Tick(object sender, EventArgs e)
{
Seconds--;
if (Seconds < 0)
{
// 停止倒计时
timer1.Stop();
return;
}
ShowTime();
}
}
}