2013-09-29 32 views
0

的代碼是:我如何讓計時器滴答活動中的計數器計數5分鐘?

private void timer1_Tick(object sender, EventArgs e) 
     { 
      count += 1; 
      countBack --; 
      TimerCount.Text = TimeSpan.FromSeconds(countBack).ToString(); 
      TimerCount.Visible = true; 
      if (count == 300) 
      { 
       timer1.Enabled = false; 
       count = 0; 
      } 
     } 

我之前變量count用於計數到5分鐘。 但我只想使用countBack變量從5分鐘計數到0,並在TimerCount中顯示它。

countBack是int並且在構造函數中,我只是將它設置爲5. countBack = 5;

我怎麼能讓我只使用countBack,當它到達0停止計時器重置countBack爲5,並顯示CountCack TimerCount?

回答

2
private void timer1_Tick(object sender, EventArgs e) 
    { 
     count ++; 
     countBack = 5 - count/60; 
     TimerCount.Text = TimeSpan.FromSeconds(count).ToString(); 
     if(!TimerCount.Visible) TimerCount.Visible = true; 
     if (count == 300){ 
      timer1.Enabled = false; 
      count = 0; 
     } 
    } 

注意:上面的代碼只是一個簡單的修改你的代碼,我們可以用更具體的方面做的更好。