C# 6.定时器 timer

使用控件:

开启定时器:timer1.Start();

关闭定时器:timer1.Stop();

定时间时间间隔:Interval

timer1.Interval = 1000;

Interva等于1000是每一秒刷新一次

定时器默认时间间隔是100ms

代码创建定时器

①创建

Timer t1 = new Timer();

②间隔

t1.Interval = 1000;

③开启

t1.Start();

④事件

t1.Tick += T1_Tick;

获取当前时间

DateTime now = DateTime.Now;

label1.Text = now.ToString("F") 显示

作业:用定时器计算距离中秋节还有多少天

label显示

①间隔②开启③目标时间④现在时间5用目标时间减去现在时间,label显示剩余的时间

cs 复制代码
 public Form1()
 {
     InitializeComponent();
     timer1.Interval = 1000;
     timer1.Enabled = true;
 }

 private void timer1_Tick(object sender, EventArgs e)
 {
     DateTime fjtime = Convert.ToDateTime("2024-09-17 00:00:00");
     DateTime xztime = Convert.ToDateTime((DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss"));
     string[] times = ((fjtime - xztime).ToString()).Split(':');
     string[] y1 = ((times[0].ToString())).Split('.');
     label1.Text = "距离放假还有" + y1[0]+"天" +y1[1] + "小时" + times[1] + "分钟" + times[2] + "秒";
 }

效果

相关推荐
weixin_428005303 小时前
C#调用 AI学习从0开始-第1阶段(基础与工具)-第2天Prompt工程基础
人工智能·学习·c#·prompt
咩图4 小时前
WPF-VisualStudio-C#-Fluent.Ribbon8.0.0学习
c#·wpf·visual studio
加号35 小时前
【C#】WPF基于Halcon 的HWindowControlWPF 控件实现图像缩放、移动
开发语言·c#·wpf
雪豹阿伟6 小时前
2.C# —— 结构体、类型转换与运算符
c#·上位机
njsgcs8 小时前
c# solidworks GetPartBox无法获得正确实体边界框原因
开发语言·c#·solidworks
rockey6278 小时前
AScript之匿名类型与动态类型
c#·.net·script·eval·expression·动态脚本
99乘法口诀万物皆可变8 小时前
BMS HIL 自动化测试框架方案(基于 CANoe + C# + Excel)
开发语言·c#·excel
祀爱9 小时前
定时任务之BackgroundService的详细教程
后端·c#·asp.net
weixin_4280053010 小时前
C#调用 AI学习从0开始-第1阶段(基础与工具)-第3天FewShot少样本测试
人工智能·c#
思麟呀10 小时前
在C++基础上理解CSharp-1
开发语言·c++·c#