用C#写一个秒表和时钟

本文介绍如何使用C#和Visual Studio 2019创建一个秒表和时钟的桌面应用程序。通过引入Diagnostics类库,学习如何利用其计时功能。在程序中,首先添加Diagnostics引用,然后利用using语句简化类库方法的调用。通过自动生成的设计器配置信息,完成窗体程序的设计。

利用C#及visual studio 2019的可视化编程写一个秒表和时钟的窗体程序。
在秒表中,需要引用一个Diagnostics的类库实现计时功能:using System.Diagnostics;
在这次编写小程序中,学会引用类库中强大的功能
主要步骤如下在这里插入图片描述
右击添加引用,搜索Diagnostics,选定添加
在程序开头可写上using System.Diagnostics;Diagnostics是该类库的命名空间(namespace),写上using System.Diagnostics;后,可以在以后在该程序应用Diagnostics类库的方法 成员时,进行简写。
类库,是将一系列常用方法的代码封装起来,方便别人直接调用

using System;
using System.Windows.Forms;
using System.Diagnostics;

namespace 计时器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void buttonTime_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();//输出当前的时间
        }
        Stopwatch sw = new Stopwatch();
        private void buttonStart_Click(object sender, EventArgs e)
        {
            sw.Start();//开始计时
            timer2.Start();
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = sw.Elapsed;//输出时间的变化
            label1.Text = ts.ToString();
            timer1.Stop();//在秒表功能运行时停止时钟运行
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            sw.Stop();//停止计时
            timer2.Stop();
        }

       
    }
}

设计器自动生成的配置信息如下:`namespace 计时器
{
partial class Form1
{
///
/// 必需的设计器变量。
///
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.label1 = new System.Windows.Forms.Label();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.button2 = new System.Windows.Forms.Button();
        this.timer2 = new System.Windows.Forms.Timer(this.components);
        this.buttonStop = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(34, 219);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(166, 40);
        this.button1.TabIndex = 1;
        this.button1.Text = "显示时间";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.buttonTime_Click);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(313, 135);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(15, 15);
        this.label1.TabIndex = 2;
        this.label1.Text = " ";
        this.label1.Click += new System.EventHandler(this.label1_Click);
        // 
        // timer1
        // 
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(258, 219);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(176, 40);
        this.button2.TabIndex = 3;
        this.button2.Text = "秒表";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.buttonStart_Click);
        // 
        // timer2
        // 
        this.timer2.Interval = 10;
        this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
        // 
        // buttonStop
        // 
        this.buttonStop.Location = new System.Drawing.Point(501, 219);
        this.buttonStop.Name = "buttonStop";
        this.buttonStop.Size = new System.Drawing.Size(101, 40);
        this.buttonStop.TabIndex = 4;
        this.buttonStop.Text = "暂停";
        this.buttonStop.UseVisualStyleBackColor = true;
        this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Controls.Add(this.buttonStop);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Timer timer2;
    private System.Windows.Forms.Button buttonStop;
}

}`

namespace StopWatch { public partial class Form2 : Form { DateTime examtime; DateTime nowtime; DateTime t = DateTime.Now; int add = 0; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { timer1.Enabled = false; examtime = new DateTime(1, 1, 1, 0,0, 0); labTime.Text = "0" + examtime.ToLongTimeString(); if (t.Hour < 10) labT.Text = "0" + DateTime.Now.ToLongTimeString(); else labT.Text = DateTime.Now.ToLongTimeString(); } private void button2_Click(object sender, EventArgs e) { if (add < 1 ) { starttime(); } else { MessageBox.Show("记录已满,请清除记录后再开始记录!"); } } private void button1_Click(object sender, EventArgs e) { if (timer1.Enabled != false) { Add(); } if (add >= 5) { MessageBox.Show("对不起!最多只能记录4条!!"); timer1.Enabled = false; } } private void button3_Click(object sender, EventArgs e) { ClearTime(); add = 0 - 1; Add(); stoptime(); } private void button4_Click(object sender, EventArgs e) { Application.Exit(); } private void timer1_Tick(object sender, EventArgs e) { examtime = examtime.AddSeconds(1); if (examtime.Hour < 10) labTime.Text = "0" + examtime.ToLongTimeString(); else labTime.Text = examtime.ToLongTimeString(); } private void timer2_Tick(object sender, EventArgs e) { if(t.Hour<10) labT.Text = "0"+DateTime.Now.ToLongTimeString(); else labT.Text = DateTime.Now.ToLongTimeString();nowtime = nowtime.AddSeconds(1); } private void Add() { add = add + 1; switch (add) { case 1: textBox1.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = true; break; case 2: textBox2.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = true; break; case 3: textBox3.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = true; break; case 4: textBox4.Text = "0" + examtime.ToLongTimeString(); Program.hash.add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text); timer1.Enabled = true; timer1.Enabled = false; break; case 5: default: break; } } private void ClearTime() { examtime = new DateTime(1, 1, 1, 0, 0, 0); labTime.Text = "0" + examtime.ToLongTimeString(); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox1.Text = "0" + examtime.ToLongTimeString(); textBox2.Text = "0" + examtime.ToLongTimeString(); textBox3.Text = "0" + examtime.ToLongTimeString(); textBox4.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = false; } private void starttime() { timer1.Enabled = true; } private void stoptime() { timer1.Enabled = false; } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } private void textBox3_TextChanged(object sender, EventArgs e) { } private void textBox4_TextChanged(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void textBox5_TextChanged(object sender, EventArgs e) { } private void labTime_Click(object sender, EventArgs e) { } private void timeshow_Click(object sender, EventArgs e) { Form3 frm = new Form3(); frm.Show(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值