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

TimeSpan ts = new TimeSpan(, , );

TimeSpan(hour,minute,second);

然后拖进去一个timer,叫timer1

timer1.Interval=1000;

设置一秒一个周期

然后在timer的事件里这样写

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

界面设计

namespace Countdown
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "开启页面";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// button2
//
this.button2.Location = new System.Drawing.Point(, );
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(, );
this.button2.TabIndex = ;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button2;
}
}

界面后台

using System;
using System.Windows.Forms; namespace Countdown
{
public partial class Form1 : Form
{
private TimeSpan ts = new TimeSpan(, , );
private Form2 frm2 = null; public Form1()
{
InitializeComponent(); } private void button1_Click(object sender, EventArgs e)
{
ts = new TimeSpan(, , );
timer1.Enabled = true; frm2 = new Form2();
frm2.ShowDialog(this); } private void timer1_Tick(object sender, EventArgs e)
{
String str = ts.Seconds.ToString();
frm2.current_time = str; ts = ts.Subtract(new TimeSpan(, , ));
if (ts.TotalSeconds < 0.0 && !frm2.EnterPhone)
{
timer1.Enabled = false;
frm2.Close();
} if (frm2.EnterPhone)
{
timer1.Enabled = false;
}
} private void button2_Click(object sender, EventArgs e)
{
Form4 f4 = new Form4();
f4.ShowDialog();
}
}
}

跳转界面的设计

namespace Countdown
{
partial class Form4
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 25F);
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "99S";
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form4
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.label1);
this.Name = "Form4";
this.Text = "Form4";
this.Load += new System.EventHandler(this.Form4_Load);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1;
private System.Windows.Forms.Timer timer1;
}
}

跳转界面的后台

using System;
using System.Windows.Forms; namespace Countdown
{
public partial class Form4 : Form
{ private TimeSpan ts = new TimeSpan(, , );
public Form4()
{
InitializeComponent();
} private void Form4_Load(object sender, EventArgs e)
{ timer1.Enabled = true;
timer1.Start(); ts = new TimeSpan(, , ); } private void timer1_Tick(object sender, EventArgs e)
{
String str = ts.TotalSeconds.ToString();//ts.Seconds.ToString();
ts = ts.Subtract(new TimeSpan(, , ));
label1.Text = str;
if (ts.TotalSeconds < 0.0 )
{
timer1.Enabled = false;
this.Close();
} }
}
}

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. HTTPS加密越来越流行,为何要加密?

    继谷歌之后,国内最大的搜索引擎百度在2015年5月实现了全站HTTPS加密.搜狗搜索.360搜索.bing搜索.淘宝.天猫.知乎等也都实现了全站HTTPS加密,互联网即将迎来全网HTTPS加密时代. ...

  2. Windows安装Apache2.4和PHP5.6

    VC11 下载安装http://www.microsoft.com/en-us/download/details.aspx?id=30679 ================= PHP(5.6) VC ...

  3. 分布式理论(五)—— 一致性算法 Paxos

    前言 Paxos 算法如同我们标题大图:世界上只有一种一致性算法,就是 Paxos.出自一位 google 大神之口. 同时,Paxos 也是出名的晦涩难懂,推理过程极其复杂.楼主在尝试理解 Paxo ...

  4. 并发编程之 CountDown 源码分析

    前言 Doug Lea 大神在 JUC 包中为我们准备了大量的多线程工具,其中包括 CountDownLatch ,名为倒计时门栓,好像不太好理解.不过,今天的文章之后,我们就彻底理解了. 如何使用? ...

  5. uni-app初体验及打包成apk

    首先用HBuilderX新建建一个uni-app项目 新建一个目录ucenter,该目录下新建两个vue文件ucenter.vue和setting.vue ucenter.vue <templa ...

  6. 聊聊Java内存模型

    一.Java内存模型 硬件处理 电脑硬件,我们知道有用于计算的cpu.辅助运算的内存.以及硬盘还有进行数据传输的数据总线.在程序执行中很多都是内存计算,cpu为了更快的进行计算会有高速缓存,最后同步至 ...

  7. SQL Server 如何添加删除外键、主键,以及更新自增属性

    1.添加删除主键和外键 例如: -----删除主键约束DECLARE @NAME SYSNAMEDECLARE @TB_NAME SYSNAMESET @TB_NAME = 'Date'SELECT ...

  8. ORACLE查看数据库已安装补丁

    cd $ORACLE_HOME ./opatch lsinventory :}

  9. 自定义MVC框架之工具类-模型类

    截止目前已经改造了5个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 自定义MVC框架之工具类-图像处理 ...

  10. Django中连接redis

    1. 安装 pip install django-redis 2. settings中配置 CACHES = { "default": { "BACKEND": ...