c#中有一个叫做timespan的数据类型,可以这样构造:

  1. TimeSpan ts = new TimeSpan(, , );

TimeSpan(hour,minute,second);

然后拖进去一个timer,叫timer1

timer1.Interval=1000;

设置一秒一个周期

然后在timer的事件里这样写

  1. private void timer1_Tick(object sender, EventArgs e)
  2. {
  3. String str = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();
  4. label1.Text = str;//label1用来显示剩余的时间
  5. ts = ts.Subtract(new TimeSpan(, , ));//每隔一秒减去一秒
  6. if (ts.TotalSeconds < 0.0)//当倒计时完毕
  7. {
  8. timer1.Enabled = false;
  9. MessageBox.Show("考试时间到,系统将强行交卷");//提示时间到,下面可以加你想要的操作
  10. }
  11. }

界面设计

  1. namespace Countdown
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// 必需的设计器变量。
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// 清理所有正在使用的资源。
  12. /// </summary>
  13. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows 窗体设计器生成的代码
  24.  
  25. /// <summary>
  26. /// 设计器支持所需的方法 - 不要修改
  27. /// 使用代码编辑器修改此方法的内容。
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.components = new System.ComponentModel.Container();
  32. this.button1 = new System.Windows.Forms.Button();
  33. this.timer1 = new System.Windows.Forms.Timer(this.components);
  34. this.button2 = new System.Windows.Forms.Button();
  35. this.SuspendLayout();
  36. //
  37. // button1
  38. //
  39. this.button1.Location = new System.Drawing.Point(, );
  40. this.button1.Name = "button1";
  41. this.button1.Size = new System.Drawing.Size(, );
  42. this.button1.TabIndex = ;
  43. this.button1.Text = "开启页面";
  44. this.button1.UseVisualStyleBackColor = true;
  45. this.button1.Click += new System.EventHandler(this.button1_Click);
  46. //
  47. // timer1
  48. //
  49. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  50. //
  51. // button2
  52. //
  53. this.button2.Location = new System.Drawing.Point(, );
  54. this.button2.Name = "button2";
  55. this.button2.Size = new System.Drawing.Size(, );
  56. this.button2.TabIndex = ;
  57. this.button2.Text = "button2";
  58. this.button2.UseVisualStyleBackColor = true;
  59. this.button2.Click += new System.EventHandler(this.button2_Click);
  60. //
  61. // Form1
  62. //
  63. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  64. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  65. this.ClientSize = new System.Drawing.Size(, );
  66. this.Controls.Add(this.button2);
  67. this.Controls.Add(this.button1);
  68. this.Name = "Form1";
  69. this.Text = "Form1";
  70. this.ResumeLayout(false);
  71.  
  72. }
  73.  
  74. #endregion
  75.  
  76. private System.Windows.Forms.Button button1;
  77. private System.Windows.Forms.Timer timer1;
  78. private System.Windows.Forms.Button button2;
  79. }
  80. }

界面后台

  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace Countdown
  5. {
  6. public partial class Form1 : Form
  7. {
  8. private TimeSpan ts = new TimeSpan(, , );
  9. private Form2 frm2 = null;
  10.  
  11. public Form1()
  12. {
  13. InitializeComponent();
  14.  
  15. }
  16.  
  17. private void button1_Click(object sender, EventArgs e)
  18. {
  19. ts = new TimeSpan(, , );
  20. timer1.Enabled = true;
  21.  
  22. frm2 = new Form2();
  23. frm2.ShowDialog(this);
  24.  
  25. }
  26.  
  27. private void timer1_Tick(object sender, EventArgs e)
  28. {
  29. String str = ts.Seconds.ToString();
  30. frm2.current_time = str;
  31.  
  32. ts = ts.Subtract(new TimeSpan(, , ));
  33. if (ts.TotalSeconds < 0.0 && !frm2.EnterPhone)
  34. {
  35. timer1.Enabled = false;
  36. frm2.Close();
  37. }
  38.  
  39. if (frm2.EnterPhone)
  40. {
  41. timer1.Enabled = false;
  42. }
  43. }
  44.  
  45. private void button2_Click(object sender, EventArgs e)
  46. {
  47. Form4 f4 = new Form4();
  48. f4.ShowDialog();
  49. }
  50. }
  51. }

跳转界面的设计

  1. namespace Countdown
  2. {
  3. partial class Form4
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.components = new System.ComponentModel.Container();
  32. this.label1 = new System.Windows.Forms.Label();
  33. this.timer1 = new System.Windows.Forms.Timer(this.components);
  34. this.SuspendLayout();
  35. //
  36. // label1
  37. //
  38. this.label1.AutoSize = true;
  39. this.label1.Font = new System.Drawing.Font("宋体", 25F);
  40. this.label1.ForeColor = System.Drawing.Color.Red;
  41. this.label1.Location = new System.Drawing.Point(, );
  42. this.label1.Name = "label1";
  43. this.label1.Size = new System.Drawing.Size(, );
  44. this.label1.TabIndex = ;
  45. this.label1.Text = "99S";
  46. //
  47. // timer1
  48. //
  49. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  50. //
  51. // Form4
  52. //
  53. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  54. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  55. this.ClientSize = new System.Drawing.Size(, );
  56. this.Controls.Add(this.label1);
  57. this.Name = "Form4";
  58. this.Text = "Form4";
  59. this.Load += new System.EventHandler(this.Form4_Load);
  60. this.ResumeLayout(false);
  61. this.PerformLayout();
  62.  
  63. }
  64.  
  65. #endregion
  66.  
  67. private System.Windows.Forms.Label label1;
  68. private System.Windows.Forms.Timer timer1;
  69. }
  70. }

跳转界面的后台

  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace Countdown
  5. {
  6. public partial class Form4 : Form
  7. {
  8.  
  9. private TimeSpan ts = new TimeSpan(, , );
  10. public Form4()
  11. {
  12. InitializeComponent();
  13. }
  14.  
  15. private void Form4_Load(object sender, EventArgs e)
  16. {
  17.  
  18. timer1.Enabled = true;
  19. timer1.Start();
  20.  
  21. ts = new TimeSpan(, , );
  22.  
  23. }
  24.  
  25. private void timer1_Tick(object sender, EventArgs e)
  26. {
  27. String str = ts.TotalSeconds.ToString();//ts.Seconds.ToString();
  28. ts = ts.Subtract(new TimeSpan(, , ));
  29. label1.Text = str;
  30. if (ts.TotalSeconds < 0.0 )
  31. {
  32. timer1.Enabled = false;
  33. this.Close();
  34. }
  35.  
  36. }
  37. }
  38. }

C# 倒计时的更多相关文章

  1. 微信小程序中利用时间选择器和js无计算实现定时器(将字符串或秒数转换成倒计时)

    转载注明出处 改成了一个单独的js文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...

  2. Android开发案例 – 在AbsListView中使用倒计时

    在App中, 有多种多样的倒计时需求, 比如: 在单View上, 使用倒计时, 如(如图-1) 在ListView(或者GridView)的ItemView上, 使用倒计时(如图-2) 图-1 图-2 ...

  3. JS案例之3——倒计时

    利用简单的数字累加循环模拟倒计时的效果,逻辑比较简单.如果大牛们有更好的办法欢迎补充. 这种效果经常用于在规定的时间做某件事.比如在1分钟之后重新发送验证码等. 案例演示: 源代码如下: <!D ...

  4. js倒计时-倒计输入的时间

    计算指定时间到指定时间之间相差多少天.时.分.秒. 节日.活动.商城常用. 原理: 主要使用到时间戳,也就是从1970 年 1 月 1 日 到指定时间的毫秒数. 1. 求出毫秒差 :当两个时间直接进行 ...

  5. 微信小程序定时器组件(输入时间字符串即可倒计时)

    昨天写了代码,今天发现要重用,干脆就抽出来做个组件得了,顺便还改善了一下代码通用性. 昨天的代码在这里 github下载地址 用法: 引入: var timer = require('../../pl ...

  6. jQuery获取短信验证码+倒计时实现

    jQuery 短信验证码倒计时 <script type="text/javascript" charset="utf-8"> $(function ...

  7. 纯js实现10分钟倒计时

    一个简单实现倒计时的小栗子~ 效果图:简陋的不能再简陋了,捂脸 代码: <!DOCTYPE HTML> <html> <head> <title> 倒计 ...

  8. Android 在线订单倒计时设计

        接到一个需求,用户下单后,商店这边需要显示在线订单列表,订单十分钟内有效.于是需要设计倒计时,显示每个订单剩余处理时间.       倒计时剩余时间: 订单创建时间 + 10分钟  - 系统当 ...

  9. js获取手机验证码倒计时的实现

    方案一 <div class="div user-input"> <input type="number" class="code& ...

  10. JavaScript倒计时

    倒计时: 1.设置一个有效的结束日期 2.计算剩余时间 3.将时间转换成可用的格式 4.输出时钟数据作为一个可重用的对象 5.在页面上显示时钟,并在它到达0时停止 <div id="c ...

随机推荐

  1. docker namespaces

    https://docs.docker.com/engine/security/userns-remap/#prerequisites 注:以下验证环境为centos7.5 docker 18.09. ...

  2. 复刻smartbits的国产网络测试工具minismb-如何测试路由器

    复刻smartbits的国产网络性能测试工具minismb,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此工具测试任何ip网络设备的端口吞吐率,带宽,并发连接数 ...

  3. MySQL进程-状态等参数详解

    查看连接 mysql -uroot -p123456 -e "show processlist;" 查看正在执行的sql语句 mysql -uroot -p123456 -e &q ...

  4. UVA 1605 Building for UN(思维)

    题目链接: https://cn.vjudge.net/problem/UVA-1605#author=0 /* 问题 设计一个包含若干层的联合国大厦,其中每一层都是等大的网格,每个格子分配给一个国家 ...

  5. node.js遇到的问题

    1.cann't find module 'request'   不能找到’request' 模块 解决方法:找到项目的根路径,cd到该路径,运行命令 npm install request 2.no ...

  6. PHP错误集锦

    错误原因:分页数据不够,使用了缓存,但是model类中又没有添加缓存.解决办法:在model类中添加缓存,内容结束时清空缓存.

  7. winform窗体 小程序【进程】

    进程 一个应用程序就是一个进程,我的理解是,只要是打开应用程序,就会创建进程. 在.NET框架在using.System.Diagnostics名称空间中,有一个类Process,用来创建一个新的进程 ...

  8. SQL - 先安装SQL2008 R2后安装AD导致无法正常登陆数据库(无法启动MSSQLSERVER)

    分析原因:安装AD后,系统改为使用域用户登陆,原先安装SQL时设置的“本地用户”信息已经修改,当前(域)用户没有权限访问MSSQLSERVER实例文件夹或整个SQL文件夹. 解决方法: 1.打开“服务 ...

  9. [日常] Go语言圣经--接口约定习题2

    练习 7.3: 为在gopl.io/ch4/treesort (§4.4)的*tree类型实现一个String方法去展示tree类型的值序列. package main import( "f ...

  10. Expression<Func<T>>和Func<T>

    以前用EF的时候,由于where的时候有Expression<Func<T>>和Func<T>两种查询条件,误用了Func<T>那个重载,后来还想通过f ...