当前位置: 首页 > news >正文

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

效果

http://www.lryc.cn/news/412894.html

相关文章:

  • 有了 createSlice,还有必要使用 createReducer 吗?什么情况需要 createReducer 呢?
  • 怎么搭建AI带货直播间生成虚拟主播?
  • 设计模式的原则
  • RocketMQ与RabbitMQ的区别:技术选型指南
  • 小白也能懂:SQL注入攻击基础与防护指南
  • 【Hot100】LeetCode—76. 最小覆盖子串
  • 删除排序链表中的重复元素 II(LeetCode)
  • 【Java】解决如何将Http转为Https加密输出
  • 二叉树链式结构的实现(递归的暴力美学!!)
  • Python | Leetcode Python题解之第312题戳气球
  • 远程访问mysql数据库的正确打开方式
  • 网络6 -- udp_socket 实现 echo服务器
  • ASUS/华硕幻15 2020 冰刃4 GX502L GU502L系列 原厂win10系统 工厂文件 带F12 ASUS Recovery恢复
  • simulink绘制bode图
  • 知识工程视角下的软件研发
  • 深度学习------权重衰退
  • 【算法】退火算法 Simulated Annealing
  • 深入理解 Git `git add -p` 命令中的交互选项
  • HTML JavaScript 闪光涟漪
  • FastAPI之Depends
  • AttributeError: module ‘jwt‘ has no attribute ‘decode‘解决方案
  • C++——C++11
  • day12 多线程
  • DeferredResult 是如何实现异步处理请求的
  • VUE3——001(03)、开发环境配置(node.js/mvn/java/ngix/tomact/vue3)
  • TCP/IP_TCP协议
  • 鸿蒙应用框架开发【简单时钟】 UI框架
  • MySQL是如何实现数据排序的
  • 【测试架构师修炼之道】读书笔记
  • C++ Functor仿函数