C#基础之秒表的简单实现

由于目前用到了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 窗体设计器生成的代码


        ///


        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        ///

        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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部