由於目前用到了C#的有關知識,但之前沒有C#的基礎,所以趁著機會正好學習學習。
本篇博文,記錄下利用C#實現壹個簡單的秒表計時器,基本界面如下圖。
功能說明:點擊“開始”開始計時,點擊“暫停”暫停計時,點擊“”停止“”停止計時,再點擊“開始”,重新開始計時。
首先,我們在窗體設計窗口畫出該界面,由1個Label,3個button構成。雙擊按鈕添加事件。
核心部分是用秒表對象Stopwatch和時鐘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;
using System.Diagnostics;
namespace Ch04Ex04
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Timer time = new Timer();
Stopwatch sw; //秒表對象
TimeSpan ts;
static int count = 1;
private void button1_Click(object sender, EventArgs e)
{
//開始按鈕
button2.Enabled = true;
button3.Enabled = true;
if(button2.Text == "繼續") //開始後將繼續按鈕重置為暫停
button2.Text = "暫停";
sw = new Stopwatch();
time.Tick += new EventHandler(time_Tick); ?//時鐘觸發信號
time.Interval = 1;
sw.Start();
time.Start();
}
void time_Tick(object sender, EventArgs e)
{
ts = sw.Elapsed;
label1.Text = string.Format("{0}:{1}:{2}:{3}", ts.Hours, ts.Minutes, ts.Seconds,ts.Milliseconds/10);
}
private void button3_Click(object sender, EventArgs e)
{
//停止時間按鈕
sw.Stop();
time.Stop();
label1.Text = string.Format("{0}:{1}:{2}:{3}", 0, 0, 0, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
button2.Enabled = false;
button3.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "暫停")
{
//暫停事件按鈕
button2.Text = "繼續";
sw.Stop();
time.Stop();
}
else if (button2.Text == "繼續")
{
//繼續事件
button2.Text = "暫停";
sw.Start();
time.Start();
}
}
}
}
秒表運行結果如圖所示。
下壹步工作:在左下方添加壹個label,實驗多次暫停的功能,即能保存秒表的多個中間結果,如記錄多名同學的長跑成績的時候,暫停按鈕只是記錄到達終點的同學的成績,計時還在繼續,這個功能不難實現,給自己也給各位壹個動手的余地。
-------------------------------------------------------------------------------------------------------
以下是窗體設計器自動生成的代碼,輔助參考。
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(204, 78);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(38, 23);
this.button1.TabIndex = 0;
this.button1.Text = "開始";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(205, 163);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(37, 23);
this.button3.TabIndex = 2;
this.button3.Text = "停止";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label1
//
this.label1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.label1.Font = new System.Drawing.Font("宋體", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(30, 33);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(202, 42);
this.label1.TabIndex = 3;
this.label1.Text = "0:0:0:0";
//
// button2
//
this.button2.Location = new System.Drawing.Point(205, 122);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(38, 23);
this.button2.TabIndex = 4;
this.button2.Text = "暫停";
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(284, 262);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.RightToLeftLayout = true;
this.Text = "秒表";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion