Seconds CountDown计时器

我有一个int值为60的lblCountdown。我想让lblCountDown的int值减少几秒,直到达到0。

这是我到目前为止:

private int counter = 60; private void button1_Click(object sender, EventArgs e) { int counter = 60; timer1 = new Timer(); timer1.Tick += new EventHandler(timer1_Tick); timer1.Interval = 1000; // 1 second timer1.Start(); label1.Text = counter.ToString(); } private void timer1_Tick(object sender, EventArgs e) { counter--; if (counter == 0) timer1.Stop(); label1.Text = counter.ToString(); } 

使用Timer来做到这一点

  private System.Windows.Forms.Timer timer1; private int counter = 60; private void btnStart_Click_1(object sender, EventArgs e) { timer1 = new System.Windows.Forms.Timer(); timer1.Tick += new EventHandler(timer1_Tick); timer1.Interval = 1000; // 1 second timer1.Start(); lblCountDown.Text = counter.ToString(); } private void timer1_Tick(object sender, EventArgs e) { counter--; if (counter == 0) timer1.Stop(); lblCountDown.Text = counter.ToString(); } 
 int segundo = 0; DateTime dt = new DateTime(); private void timer1_Tick(object sender, EventArgs e){ segundo++; label1.Text = dt.AddSeconds(segundo).ToString("HH:mm:ss"); } 

你需要使用一个Timer并挂钩到Tick事件来做你想要的事情。 http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

如何使用.NET Framework中的Timer类? http://msdn.microsoft.com/en-us/library/0tcs6ww8.aspx

您需要Form1的公共类进行初始化。

看到这段代码:

 namespace TimerApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int counter = 60; private void button1_Click(object sender, EventArgs e) { //Insert your code from before } private void timer1_Tick(object sender, EventArgs e) { //Again insert your code } } } 

我试过这个,一切都很好

如果您需要帮助,请随时评论:)