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

效果

相关推荐
Chen—LSN5 小时前
C语言——文件操作(2)
c语言·开发语言·c++·经验分享·笔记·算法·c#
阿钱真强道7 小时前
18 嵌入式操作系统 | uloop 定时器:周期任务与网络上报
c语言·定时器·周期任务·uloop
淡海水8 小时前
00-Unity和C#的GC剖析与对比-全篇导览
unity·c#·游戏引擎·gc
fogota8 小时前
C# `string.Join` 处理 `List<int>`
windows·c#
Behavior11 小时前
Unity 手游动态更换 App 图标 — Android 与 iOS 双端技术方案
c#·unity3d·游戏开发
周杰伦fans11 小时前
构建真正智能的CAD AI绘图代理
开发语言·人工智能·c#
UIU11415 小时前
递归算法与汉诺塔
c++·学习·算法·c#·递归
CSharp精选营20 小时前
我用 ASP.NET Core 做了个水稻病虫害检查系统
c#·毕业设计·.net core·码农刚子
唐青枫20 小时前
我用 C# 开发了一款电子发票自动整理工具
c#·.net
下页、再停留20 小时前
【C#桌面客户端系列学习-2】在原来窗口基础上打开新窗口
c#·visual studio