以特定格式显示计时器间隔刷新

时间:2019-03-18 08:55:25

标签: c# winforms

我在项目(win窗体)中以1个小时的间隔使用虚拟计时器,我可以在Windows窗体中的倒计时模式下以这种格式{“ hour”:“ min”:“ sec”}看到用户的时间。你能帮助我吗?谢谢。

Timer Reset_timer = new Timer();
        private void chkactive_CheckedChanged(object sender, EventArgs e)
    {
        if (chkactive.Checked == true)
        {
            Reset_timer.Interval = (int)nmtimervalue.Value * 10000;                                                                                              
            Reset_timer.Enabled = true;
            sw.Start();
        }
        if (chkactive.Checked == false)
            Reset_timer.Enabled = false;
    }

1 个答案:

答案 0 :(得分:0)

基本上,您需要倒数计时模式。

1st:在winforms上创建一个计时器。

在此之后,声明一个新变量,例如private int a = 1800; // 1800/60 = 30 seconds.

(如果您希望1分钟放置private int a = 3600;(3600/60)= 60秒。)

2nd:启动计时器的按钮添加以下代码:

timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;

第3次:在您的timer1_Tick上:

a--; //decrementing the timer 1 sec
resultLabel.Text = a / 60 + ":" + ((a % 60) >= 10 ? (a % 60).ToString() : "0" + (a % 60)); //this is a function who gonna returns the remainder of dividing the value of a for 60(seconds), basically to get that smooth countdown ..

第4个:用于停止计时器的按钮:

timer1.Stop();