利用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;
}
}`