using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace Clock
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Timer time = new Timer();
Stopwatch sw; //秒表对象
TimeSpan ts;
static int count = 1;
private void Form1_Load(object sender, EventArgs e)
{
button2.Enabled = false;
button3.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
//开始按钮
button2.Enabled = true;
button3.Enabled = false;
if (button2.Text == "继续")
button2.Text = "暂停";
sw = new Stopwatch();
time.Tick += new EventHandler(timer1_Tick); //触发
time.Interval = 1;
sw.Start();
time.Start();
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "暂停")
{
//暂停
button2.Text = "继续";
sw.Stop();
time.Stop();
button3.Enabled = true;
}
else if (button2.Text == "继续")
{
//继续
button2.Text = "暂停";
sw.Start();
time.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
ts = sw.Elapsed;
label1.Text = string.Format("{0}:{1}:{2}:{3}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
}
private void button3_Click(object sender, EventArgs e)
{
//归零
sw.Stop();
time.Stop();
label1.Text = string.Format("{0}:{1}:{2}:{3}", 0, 0, 0, 0);
button2.Enabled = false;
button1.Enabled = true;
}
}
}
C#窗体应用程序miaobiao
最新推荐文章于 2024-06-04 22:15:07 发布
这篇博客展示了如何在C#中利用Stopwatch和Timer类创建一个计时器应用。应用包括开始、暂停和重置功能,用于显示小时、分钟、秒和毫秒。点击开始按钮启动计时,暂停按钮可以暂停计时,而重置按钮将计时器归零。
摘要由CSDN通过智能技术生成