1.UI界面
2.窗体代码 Form1.Designer.cs
namespace 倒计时器
{
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.progressBar1 = new System.Windows.Forms.ProgressBar();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
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(197, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(94, 47);
this.button1.TabIndex = 0;
this.button1.Text = "开始";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(13, 116);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(292, 23);
this.progressBar1.TabIndex = 1;
//
// comboBox1
//
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(85, 26);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(86, 20);
this.comboBox1.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(26, 30);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 3;
this.label1.Text = "定时时间";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(26, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 12);
this.label2.TabIndex = 4;
this.label2.Text = "剩余时间";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(110, 72);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(23, 12);
this.label3.TabIndex = 5;
this.label3.Text = "0秒";
this.label3.Click += new System.EventHandler(this.label3_Click);
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// button2
//
this.button2.Location = new System.Drawing.Point(197, 63);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(94, 47);
this.button2.TabIndex = 6;
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.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.ClientSize = new System.Drawing.Size(317, 162);
this.Controls.Add(this.button2);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "定时器";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button2;
}
}
3.程序代码 Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 倒计时器
{
public partial class Form1 : Form
{
int count=0;
int time;
int flag=0;
public Form1()
{
InitializeComponent();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.TabIndex = 0;
int i;
for ( i = 1; i < 100; i++)
{
comboBox1.Items.Add(i.ToString()+" 秒");//秒前面最好加一个空格
}
label3.Text = "";
}
//在属性里改成1000(一秒钟),每一秒钟过去会执行该定时中断系统
private void timer1_Tick(object sender, EventArgs e)//定时器事件
{
count++;//记录当前秒,记得属性里面改成1000
label3.Text=(time-count).ToString()+"秒";//总时长-当前时长=剩余时长,并且显示在在文本框
progressBar1.Value = count;//把当前时长的值给进度条进度
if (count==time)//时间到,
{
timer1.Stop();//停止计时
System.Media.SystemSounds.Asterisk.Play ();//提示音
//弹出显示框,注意当执行到这里,程序会停在这里,按下确认才会继续执行,所以该代码要放在计时停止的程序下面
MessageBox.Show("时间到了!", "提示");
}
}
private void button1_Click(object sender, EventArgs e)
{
string str = comboBox1.Text;//这行代码获取comboBox1控件的当前文本值,并将其赋给字符串变量str
time = Convert.ToInt32(str.Substring(0, 2));//通过截取str的前两个字符,然后将这个字符串转换成整数类型来设置time变量
progressBar1.Maximum = time;//设置进度条progressBar1的最大值为time
if (!timer1.Enabled) //如果计时器没有启用(即!timer1.Enabled),则启动计时器timer1
{
timer1.Start();
button1.Text = "清零";
label3.Text = time + "秒";
}
//如果计时器已经启用,则停止计时器timer1,重置计数器count为0,更新label3的文本为"0秒",并将进度条progressBar1的当前值设为0。
else
{
timer1.Stop();
button1.Text = "开始";
count = 0;
label3.Text = 0 + "秒";
progressBar1.Value = 0;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (flag == 0)
{
timer1.Stop();
flag = 1;
button2.Text = "继续";
}
else if (flag == 1)
{
timer1.Start ();
flag = 0;
button2.Text = "暂停";
}
}
}
}
3.运行