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

  • 相关阅读:
    箭头函数和普通函数的区别是什么?
    前端如何优化网站性能?
    instanceof原理
    call、apply区别
    函数的节流和防抖
    关于this的指向性问题
    undefined 和null的区别?
    浅谈堆和栈的理解?
    关于vue中watch和computed
    简单说一下什么是回流和重绘
  • 原文地址:https://www.cnblogs.com/hust_wsh/p/2023698.html
Copyright © 2011-2022 走看看