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

效果

相关推荐
Crazy Struggle19 小时前
告别人工干预!C# 轻量级上位机自动联动 MES 与视觉检测
c#·.net·mes
LateFrames19 小时前
做【秒开】的程序:WPF / WinForm / WinUI3 / Electron
electron·c#·wpf·winform·winui3·claude code
想拿大厂offer20 小时前
Windows Server 2022 + IIS + .NET 8 + MySQL 部署手册
c#·asp.net
小码编匠1 天前
.NET 10 性能突破:持续优化才是质变关键
后端·c#·.net
mudtools1 天前
.NET驾驭Excel之力:Excel应用程序的创建与管理
c#·.net·excel·wps
mudtools1 天前
.NET驾驭Excel之力:自动化数据处理 - 开篇概述与环境准备
c#·自动化·.net·excel·wps
唐青枫1 天前
C#.NET WebAPI 返回类型深度解析:IActionResult 与 ActionResult<T> 的区别与应用
c#·.net
William_cl1 天前
C# ASP.NET MVC 数据验证实战:View 层双保险(Html.ValidationMessageFor + jQuery Validate)
后端·c#·asp.net·mvc
狮子不白1 天前
C#WEB 防重复提交控制
开发语言·前端·程序人生·c#
Charles_go2 天前
C#8、有哪些访问修饰符
java·前端·c#