现在的位置: 首页 > 综合 > 正文

用C#做个简单的计时器(秒表功能)

2012年09月20日 ⁄ 综合 ⁄ 共 2768字 ⁄ 字号 评论关闭

        先在窗口中加入一个计时器控件:(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);
            }
            
        }

抱歉!评论已关闭.