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

效果

相关推荐
鱼听禅3 小时前
C# 学习笔记-使用 类型化客户端搭建HTTP客户端服务
学习·http·c#·工厂方法模式
geovindu5 小时前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
淡海水5 小时前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
fogota6 小时前
C# 如何调用系统图标
c#·.net
△曉風殘月〆6 小时前
.NET是什么?为什么要选择.NET?
c#·.net·.net core·clr
fogota6 小时前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota6 小时前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头7 小时前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
zxy284722530117 小时前
C# 音频倍速播放
c#·音频·naudio·soundtouch·倍速
Water_Sunzhipeng21 小时前
HandyControl Demo 设置特定框架记录
c#