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] + "秒";
 }

效果

相关推荐
大侠课堂7 小时前
C#经典面试题100道
开发语言·c#
时光追逐者9 小时前
Visual Studio 2026 现已正式发布,更快、更智能!
ide·c#·.net·visual studio
周杰伦fans10 小时前
C# 正则表达式完全指南
mysql·正则表达式·c#
Triumph++12 小时前
电器模C#汇控电子继块驱动(Modbus协议)
c#·visual studio·c#串口通信
咩图16 小时前
C#创建AI项目
开发语言·人工智能·c#
周杰伦fans17 小时前
C# - Task 是什么?想象一下你在餐厅点餐
服务器·开发语言·c#
一只小小汤圆19 小时前
简化点集合 道格拉斯-普克算法(Douglas-Peucker Algorithm)
c#·occ
scixing19 小时前
函数式编程 第八讲 循环者,递归也
开发语言·c#
屠夫19 小时前
SqlSugar的简单使用
c#
dotent·1 天前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf