zoukankan      html  css  js  c++  java
  • C#计时器

    1.计时类StartTimer.cs

    View Code
    class StartTimer
    {
    public uint second;
    public uint minute;
    public uint hour;
    public Label labelTimer = new Label();
    public Timer countTimer = new Timer();//定义一个计时器
    //计时函数
    public StartTimer()
    {
    labelTimer.Size
    = new Size(120, 20);
    this.labelTimer.Text = "00:00:00";
    this.labelTimer.TextAlign = System.Drawing.ContentAlignment.TopCenter;

    countTimer.Interval
    = 1000;//计时定时器初始化
    countTimer.Tick += new EventHandler(countTimer_Tick);
    }
    private string strTimer()
    {
    string timer = null;
    string timerSecond = null;
    string timerMinute = null;
    string timerHour = null;
    if (second < 59)
    {
    second
    ++;
    }
    else
    {
    second
    = 0;
    if (minute < 59)
    {
    minute
    ++;
    }
    else
    {
    minute
    = 0;
    if (hour < 23)
    {
    hour
    ++;
    }
    else
    {
    hour
    = 0;
    }
    }
    }
    if (second < 10)
    {
    timerSecond
    = "0" + second.ToString();
    }
    else
    {
    timerSecond
    = second.ToString();
    }
    if (minute < 10)
    {
    timerMinute
    = "0" + minute.ToString();
    }
    else
    {
    timerMinute
    = minute.ToString();
    }
    if (hour < 10)
    {
    timerHour
    = "0" + hour.ToString();
    }
    else
    {
    timerHour
    = hour.ToString();
    }
    timer
    = timerHour + ":" + timerMinute + ":" + timerSecond;
    return timer;
    }
    //重置计时
    public void resetTimer()
    {
    second
    = 0;
    minute
    = 0;
    hour
    = 0;
    labelTimer.Text
    = "00:00:00";
    }
    //采集计时
    private void countTimer_Tick(object sender, EventArgs e)
    {
    labelTimer.Text
    = strTimer();
    }
    }

    2.调用

    StartTimer timer=new StartTimer();//新建一个实例

    this.Controls.Add(startTimer.labelTimer);//将时间label添加到当前的Form中
    startTimer.labelTimer.Location = new Point(120,220);//指定时间label的位置
    startTimer.countTimer.Enabled = true;//开始计时

  • 相关阅读:
    matrix_2015_1 138
    2014ACM/ICPC亚洲区广州站 北大命题
    无向图的联通分量
    5.1 基础题目选讲
    URAL
    Codeforces Round #274 (Div. 2)
    后缀数组
    poj 1661 help jimmy dp
    hdu 1676 Full Tank? 限制最短路 dp 蛮有技巧的~
    hdu 1023 Train Problem II 双向广搜
  • 原文地址:https://www.cnblogs.com/hust_wsh/p/2023698.html
Copyright © 2011-2022 走看看