前言
- C# 的应用设计非常的简单,可以开发一些上位机小工具,这里熟悉下Timer(定时器)这个控件
操作步骤
- 新建一个 C# WinForm 窗体的应用,在工具箱,拖动一个标签空间与一个Timer(定时器)空间到窗体
- Timer控件对象,默认是不开启的,可以在【属性】里面设置是否开启,可以设置定时器的时间间隔
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1ce8da279fea50d776aef4ebeddc151b.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a7596db8dd282f3551a61d8b258242f4.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/396d4a2b4f0c576d09e0c7d7a57d7e01.png)
测试代码
using System;
using System.Windows.Forms;
namespace timer_demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
lbl_dataTime.Text = DateTime.Now.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
}
}
运行效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/de854b17e1fa46b97a68e056540698e4.png)
小结
- Timer 控件使用起来很简单,设置定时器的间隔,启动定时器,定时器的回调函数,就会周期性的执行,当然可以在需要的时候停止定时器,
timer1.Stop();
- 熟悉C# 常用控件的使用,可以用C# 开发一些有价值的上位机小工具