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

效果

相关推荐
时光の尘19 小时前
【凌鸥LKS32】基础篇(二)·Timer定时器实现LED闪烁
单片机·mcu·物联网·iot·timer·lks07
神秘的MT21 小时前
C# WinForm Socket 类 常用方法 (C# .NET,TCP 场景)
服务器·网络·学习·tcp/ip·c#·winform
神罚天下ET1 天前
c# ACME client (补充)
开发语言·c#
噗噗夹的TA之旅2 天前
Shader 学习 21:自定义 Render Feature
学习·游戏·unity·c#·游戏引擎·图形渲染
脚踏实地皮皮晨2 天前
003006001_WrapPanel控件
开发语言·c#·.net·wpf·visual studio
脚踏实地皮皮晨2 天前
003005002_WPF StackPanel 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
用户298698530142 天前
Excel 转 PDF 免费在线工具,格式完美保留
人工智能·c#·excel
库拉库拉里2 天前
深入理解 C# 异步编程:同步、Task.Wait () 与 await 的本质区别及实践指南
开发语言·c#
神罚天下ET2 天前
.NET 新增功能系列文章——C# 中的新增功能
开发语言·c#·.net
caishenzhibiao2 天前
缠论画线主图 同花顺期货通指标
java·c语言·c#