C# 倒计时
c#中有一個叫做timespan的數據類型,可以這樣構造:
TimeSpan ts = new TimeSpan(0, 45, 0);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(0, 0, 1));//每隔一秒減去一秒 if (ts.TotalSeconds < 0.0)//當倒計時完畢 { timer1.Enabled = false; MessageBox.Show("考試時間到,系統將強行交卷");//提示時間到,下面可以加你想要的操作 } } View Code?
界面設計
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(179, 210);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(106, 33);this.button1.TabIndex = 0;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(55, 285);this.button2.Name = "button2";this.button2.Size = new System.Drawing.Size(75, 23);this.button2.TabIndex = 1;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(486, 337);this.Controls.Add(this.button2);this.Controls.Add(this.button1);this.Name = "Form1";this.Text = "Form1";this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.Button button1;private System.Windows.Forms.Timer timer1;private System.Windows.Forms.Button button2;} } View Code界面后臺
using System; using System.Windows.Forms;namespace Countdown {public partial class Form1 : Form{private TimeSpan ts = new TimeSpan(0, 0, 0);private Form2 frm2 = null;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){ts = new TimeSpan(0, 0, 50);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(0, 0, 1));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();}} } View Code跳轉界面的設計
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(658, 13);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(66, 34);this.label1.TabIndex = 0;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(800, 450);this.Controls.Add(this.label1);this.Name = "Form4";this.Text = "Form4";this.Load += new System.EventHandler(this.Form4_Load);this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.Label label1;private System.Windows.Forms.Timer timer1;} } View Code跳轉界面的后臺
using System; using System.Windows.Forms;namespace Countdown {public partial class Form4 : Form{private TimeSpan ts = new TimeSpan(0, 0, 0);public Form4(){InitializeComponent();}private void Form4_Load(object sender, EventArgs e){timer1.Enabled = true;timer1.Start();ts = new TimeSpan(0, 1, 39);}private void timer1_Tick(object sender, EventArgs e){String str = ts.TotalSeconds.ToString();//ts.Seconds.ToString();ts = ts.Subtract(new TimeSpan(0, 0, 1));label1.Text = str;if (ts.TotalSeconds < 0.0 ){timer1.Enabled = false;this.Close();}}} } View Code?
轉載于:https://www.cnblogs.com/aijiao/p/10763723.html
總結
- 上一篇: 正则表达式匹配两个特殊字符中间的内容
- 下一篇: c# char unsigned_dll