• 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;//开始计时

  • 相关阅读:
    py爬取英文文档学习单词
    windows 下使clion支持c++11操作记录
    angular在ie8下的一个bug
    连连看小游戏前端实现
    如何禁止页面文字被选中
    分享一个BUG
    描点链接元素的优化提升用户体验
    模拟淘宝滚动显示问题解决入口
    简易图片轮播效果
    支付战争
  • 原文地址:https://www.cnblogs.com/hust_wsh/p/2023698.html
Copyright © 2020-2023  润新知