欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > C# 6.定时器 timer

C# 6.定时器 timer

2025/3/14 20:50:55 来源:https://blog.csdn.net/weixin_73535261/article/details/140781346  浏览:    关键词: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显示剩余的时间

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

效果

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词