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

效果

相关推荐
计算机学姐33 分钟前
基于Asp.net的教学管理系统
vue.js·windows·后端·sqlserver·c#·asp.net·visual studio
且听风吟ayan1 小时前
leetcode day26 重复的子字符串
算法·leetcode·c#
caoruipeng3 小时前
Windows编程----进程的当前目录
c++·windows·c#
h20170106874 小时前
C#中的委托是什么?事件是不是一种委托?委托与事件的区别?
开发语言·c#·.net·面试题
无所谓จุ๊บ4 小时前
使用AI整理知识点--WPF动画核心知识
c#·wpf
hihaojie5 小时前
C# 中的“相等判断”
c#·总结
Whappy0015 小时前
第三节:基于Winform框架的串口助手小项目---串口操作《C#编程》
linux·单片机·c#
kylezhao20195 小时前
C# Excel开源操作库MiniExcel使用教程
开发语言·c#·excel
yuanpan6 小时前
23种设计模式之《装饰器模式(Decorator)》在c#中的应用及理解
设计模式·c#·装饰器模式
白白白白纸呀6 小时前
.NET高级应用---自定义Ioc容器(附带源码)
开发语言·c#·.net