winform实现倒计时的重置代码怎么做

zbguolei 2021-01-23 08:43:59


“开始”、“暂停”的功能已实现。


请问:“重置”----计时重新开始,如何实现?


相关代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 倒计时
{
public partial class Form1 : Form
{
TimeSpan ts = new TimeSpan(0, 2, 0); //声明时间段,结构为TimeSpan(hour,minute,second)
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void timer1_Tick(object sender, EventArgs e) //表示在设定的时间间隔后,自动触发的事件
{

ts = ts.Subtract(new TimeSpan(0, 0, 1)); //从中减去另一个TimeSpan值,每隔一秒减去一秒
string str_hr = ts.Hours > 9 ? Convert.ToString(ts.Hours) : "0" + Convert.ToString(ts.Hours);
string str_min = ts.Minutes > 9 ? Convert.ToString(ts.Minutes) : "0" + Convert.ToString(ts.Minutes);
string str_sec = ts.Seconds > 9 ? Convert.ToString(ts.Seconds) : "0" + Convert.ToString(ts.Seconds);

label1.Text = str_hr + ":" + str_min + ":" + str_sec;

if (ts.TotalSeconds <= 0.0)//当倒计时完毕
{
timer1.Enabled = false; //其中可自行添加相应的提示框或者方法函数
}
}

private void Form1_Shown(object sender, EventArgs e) //只有在首次显示窗体时才会引发 Shown 事件
{
this.WindowState = FormWindowState.Maximized; //最大化


int xWidth = SystemInformation.PrimaryMonitorSize.Width; //获取显示器屏幕宽度
int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度

label1.Font = new System.Drawing.Font("", xWidth/6);

this.button1.Location= new System.Drawing.Point(100,yHeight - 100);
this.button2.Location = new System.Drawing.Point(500, yHeight - 100);
this.button3.Location = new System.Drawing.Point(800, yHeight - 100);

}

private void button2_Click(object sender, EventArgs e) //暂停按钮
{
if (timer1.Enabled)
{
timer1.Stop();
timer1.Enabled = false;

}
}

private void button1_Click(object sender, EventArgs e) //开始按钮
{
timer1.Interval = 1000;//设置每次间隔1s
timer1.Enabled = true;
timer1.Start();
}

private void button3_Click(object sender, EventArgs e) //重置按钮
{
timer1.Stop();

}
}
}
...全文
253 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zbguolei 2021-01-23
  • 打赏
  • 举报
回复
ts是总的时长啊。
  • 打赏
  • 举报
回复
ts = new TimeSpan(0, 2, 0);
  • 打赏
  • 举报
回复
搞懂你自己代码的 ts 是干什么的即可。
zbguolei 2021-01-23
  • 打赏
  • 举报
回复
我已经调试成功了,谢谢指点,让我深入了解的变量和方法。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 倒计时
{
    public partial class Form1 : Form
    {
        TimeSpan ts = new TimeSpan(0, 1, 0);     //声明时间段,结构为TimeSpan(hour,minute,second)
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;//设置每次间隔1s
            show();
        }

        private void show()
        {
            string str_hr = ts.Hours > 9 ? Convert.ToString(ts.Hours) : "0" + Convert.ToString(ts.Hours);
            string str_min = ts.Minutes > 9 ? Convert.ToString(ts.Minutes) : "0" + Convert.ToString(ts.Minutes);
            string str_sec = ts.Seconds > 9 ? Convert.ToString(ts.Seconds) : "0" + Convert.ToString(ts.Seconds);
            label1.Text = str_hr + ":" + str_min + ":" + str_sec;
        }

        private void timer1_Tick(object sender, EventArgs e)    //表示在设定的时间间隔后,自动触发的事件
        {
            
            ts = ts.Subtract(new TimeSpan(0, 0, 1));   //从中减去另一个TimeSpan值,每隔一秒减去一秒
            show();

            if (ts.TotalSeconds <= 0.0)//当倒计时完毕
            {
                timer1.Enabled = false;   //其中可自行添加相应的提示框或者方法函数
                label1.Text = "时间到!";
            }
        }

        private void Form1_Shown(object sender, EventArgs e)        //只有在首次显示窗体时才会引发 Shown 事件
        {
            this.WindowState = FormWindowState.Maximized;   //最大化
            
            int xWidth = SystemInformation.PrimaryMonitorSize.Width; //获取显示器屏幕宽度
            int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度

            label1.Font = new System.Drawing.Font("", xWidth/6);

            this.button1.Location= new System.Drawing.Point(100,yHeight - 100);
            this.button2.Location = new System.Drawing.Point(500, yHeight - 100);
            this.button3.Location = new System.Drawing.Point(800, yHeight - 100);
            
        }

        private void button2_Click(object sender, EventArgs e)  //暂停按钮
        {
            if (timer1.Enabled)
            {
                timer1.Stop();
                timer1.Enabled = false;
                
            }
        }

        private void button1_Click(object sender, EventArgs e)      //开始按钮
        {
            
            timer1.Enabled = true;
            timer1.Start();
        }

        private void button3_Click(object sender, EventArgs e)      //重置按钮
        {
            timer1.Stop();
            ts = new TimeSpan(0, 1, 0);
            show();
        }
    }
}

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧