用C#做個簡單的計時器(秒錶功能)

來源:互聯網
上載者:User

        先在視窗中加入一個計時器控制項:(timer1)用於觸發計時;一個Label控制項:(bable1)用於顯示時間;兩個按鈕:(btnStar)用於開始/停止計時,(btnClear)用於計時器清零。

        聲明一個整型變數:t,用於擷取毫秒,然後在視窗代碼中加入以下代碼:

        
        private int t = 0;
                
        void MainFormLoad(object sender, System.EventArgs e)
        {
            this.timer1.Enabled = false;
            this.timer1.Interval = 1;
        }
            
        //計時函數
        public string GetAllTime(int time)   
          {
            string hh, mm, ss, fff;
                
              int f = time%100; // 毫秒   
            int s = time/100; // 轉化為秒
            int m = s/60;     // 分
            int h = m/60;     // 時
                  s = s%60;     // 秒 
        
                  //毫秒格式00
                  if(f<10)
                  {
                      fff = "0" + f.ToString();
                  }
                  else
                  {
                      fff = f.ToString();
                  }
                  
                  //秒格式00
                  if(s<10)
                  {
                      ss = "0" + s.ToString();
                  }
                  else
                  {
                      ss =  s.ToString();
                  }
                  
                  //分格式00
                  if(m<10)
                  {
                      mm = "0" + m.ToString();
                  }
                  else
                  {
                      mm = m.ToString();
                  }
                  
                  //時格式00
                  if(h<10)
                  {
                      hh = "0" + h.ToString();
                  }
                  else
                  {
                      hh = h.ToString();
                  }
              
              //返回 hh:mm:ss.ff            
              return hh + ":" + mm + ":" + ss + "." + fff;
          }
        
        //開始計時按鈕單擊事件
        void BtnOKClick(object sender, System.EventArgs e)
        {
            if(timer1.Enabled == false)
            {
                this.btnOK.Text = "停止計時";
                this.timer1.Enabled = true;
            }
            else
            {
                this.btnOK.Text = "開始計時";
                this.timer1.Enabled = false;
            }
        }
    
        //時鐘控制項事件
        void Timer1Tick(object sender, System.EventArgs e)
        {            
            t = t + 1;//得到總的毫秒數   
              this.label1.Text = GetAllTime(t);
        }
    
        //計時器清零
        void BtnClearClick(object sender, System.EventArgs e)
        {
            t = 0;
            //如何正在計時,則先停止再清零,否則直接清零
            if(this.timer1.Enabled == true)
            {
                this.BtnOKClick(sender,e);
                label1.Text = GetAllTime(t);
            }
            else
            {
                label1.Text = GetAllTime(t);
            }
            
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.