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

C# 计时器Timer控件,倒计时

实例一:计时器
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

    public partial class Timer : Form

    {

        public Timer()

        {

            InitializeComponent();

        }

       

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

 

        System.Timers.Timer t = new System.Timers.Timer();//实例化Timer类
 

        private void btnTimer_Click(object sender, EventArgs e)

        {

            int intTime = 3000;

            t.Interval = intTime;//设置间隔时间,为毫秒;
            t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;
            t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
        }

        public void theout(object source, System.Timers.ElapsedEventArgs e)

        {   //MessageBox.Show 一种消息提示对话框。该对象只有winform程序才有,可设置内容,标题,按钮,图标,按钮焦点等参数
            MessageBox.Show(e.SignalTime.ToString(), "标题", MessageBoxButtons.OKCancel, MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);

        }

 

        private void btnOpenForm2_Click(object sender, EventArgs e)

        {

            Form2 f = new Form2();

            f.Show();

            this.Visible = false;

        }

    }

}

实例二:倒计时功能
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

 

        int mint = 10;

        int scss = 59;

        private void Form2_Load(object sender, EventArgs e)

        {

            label1.Text = mint + "分";

            label2.Text = scss + "秒";

            this.timer1.Interval = 1000; //设置间隔时间,为毫秒;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);设置每间隔3000毫秒(3秒)执行一次函数timer1_Tick

            this.timer1.Start();

        }

 

        private void timer1_Tick(object sender, EventArgs e)

        {

            if (mint >= 0)

            {

                scss--;

                if (scss == 0)

                {

                    mint--;

                    label1.Text = mint.ToString() + "分";

                    scss = 59;

                }

                label2.Text = scss.ToString() + "秒";

            }

        }

    }

}

 

 

 

相关文章:

  • MySQL 错误Incorrect key file for table ******.MYI; try to repair it的解决
  • 关于无效使用 Null: 'Replace'
  • css透明与半透明
  • C# FTP上传文件至服务器代码
  • 利用FTPClient类实现文件的上传下载功能
  • mysql不能使用innodb存储引擎
  • ASP.NET MVC3 异常处理
  • 解决【Unable to make the session state request to the session state server】
  • 如何在IIS里对网站限速
  • msxml3.dll 错误 '80072efd' A connection with the server could not be established
  • 解决IE6浏览器中Div层挡不住Select组件
  • 解决IE6下select z-index 无效
  • Ubuntu下gcc安装及使用
  • Linux(ubuntu)下手动安装 firefox 6 并且添加快捷方式图标
  • ubuntu下如何安装使用SSH
  • JS 中的深拷贝与浅拷贝
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • Angular数据绑定机制
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • codis proxy处理流程
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • Java面向对象及其三大特征
  • js ES6 求数组的交集,并集,还有差集
  • Python进阶细节
  • Shell编程
  • 解析 Webpack中import、require、按需加载的执行过程
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 前端面试之闭包
  • 实习面试笔记
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 再次简单明了总结flex布局,一看就懂...
  • 正则表达式小结
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • ​3ds Max插件CG MAGIC图形板块为您提升线条效率!
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #define、const、typedef的差别
  • #if #elif #endif
  • (4)Elastix图像配准:3D图像
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (Forward) Music Player: From UI Proposal to Code
  • (二)JAVA使用POI操作excel
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (算法)N皇后问题
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)ObjectiveC 深浅拷贝学习
  • (转载)OpenStack Hacker养成指南
  • (转载)深入super,看Python如何解决钻石继承难题
  • * 论文笔记 【Wide Deep Learning for Recommender Systems】
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .net core webapi 大文件上传到wwwroot文件夹
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈