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

效果

相关推荐
CodeCraft Studio5 小时前
PPT处理控件Aspose.Slides教程:使用 C# 将 PPTX 转换为 EMF
c#·powerpoint·ppt·aspose·ppt格式转换
future14126 小时前
游戏开发日记7.12
数据结构·学习·c#·游戏开发
洁辉6 小时前
C# & .NET 面试深度复习指南
面试·c#·.net
_oP_i8 小时前
无法找到来自源 EdgeWebView,实际安装了,偶尔出现
c#
阿蒙Amon9 小时前
C#类型转换:从基础到进阶的全景解析
开发语言·c#
时光追逐者15 小时前
一款开源免费、通用的 WPF 主题控件包
开源·c#·.net·wpf
张人玉18 小时前
c#中Random类、DateTime类、String类
开发语言·c#
future141219 小时前
游戏开发日记
数据结构·学习·c#
军训猫猫头21 小时前
3.检查函数 if (!CheckStart()) return 的妙用 C#例子
开发语言·c#
yngsqq1 天前
netdxf—— CAD c#二次开发之(netDxf 处理 DXF 文件)
java·前端·c#