1 namespace 定时器设计
2 {
3 public partial class Form1 : Form
4 {
5 UInt16 Timer_Value = 0;//定时值
6 UInt16 Timer_Count = 0;//定时器计数器
7 byte Timer_Status = 0;//定时器状态
8 //0=停止状态 1定时状态 2为暂停状态
9 public Form1()
10 {
11 InitializeComponent();
12 }
13 // 窗体装载事件
14 private void Form1_Load(object sender, EventArgs e)
15 {
16 //分钟、秒钟组合框初始化
17 byte i;//声明一个字节变量 i
18 for(i=0;i<60;i++)
19 {
20 comboBox1.Items.Add(i.ToString()); //往组合框里面添加60个数值
21 comboBox2.Items.Add(i.ToString());//往组合框里面添加60个数值
22 }
23 //分钟、秒钟组合框默认设为0
24 comboBox1.Text = "0";
25 comboBox2.Text = "0";
26 }
27 //定时按钮单击事件
28 private void button1_Click(object sender, EventArgs e)
29 {
30 switch (Timer_Status) //定时器状态设置
31 {
32 case 0:
33 {
34 //获取定时器的时间
35 Timer_Value = Convert.ToUInt16(comboBox1.Text, 10); //将10进制的组合框中的内容转换为无符号整数型赋值到Value变量
36 Timer_Value *=60;//将获取的值每一次乘60,将分钟转换为秒
37 Timer_Value += Convert.ToUInt16(comboBox2.Text, 10);//将分钟加上组合框2中的十进制整数,不断相加分钟数
38 if (Timer_Value > 0) //如果分钟数>0
39 {
40 //开始定时任务
41 textBox1.Text = Timer_Value.ToString() + " S"; //文本框里就显示value的值+s
42 button1.Text = "暂停计时"; //按钮1显示为暂停计时
43 button2.Enabled = true; //按钮2使能
44 comboBox1.Enabled = false; //组合框1编辑失能
45 comboBox2.Enabled = false;//组合框2编辑失能
46 //更新定时器状态
47 Timer_Status = 1; //将定时器状态置1
48 //设置进度条
49 progressBar1.Value = 0; //初始化进度条为0
50 progressBar1.Maximum = Timer_Value; //进度条最大值为定时器为总秒数
51 //定时器使能
52 timer1.Start(); //定时器使能
53 }
54 else
55 {
56 System.Media.SystemSounds.Beep.Play();//调用系统提示音
57 MessageBox.Show("定时时间不能为0", "提示"); //此状态为定时时间为<0或等于0弹出信息提示框
58
59 }
60 break;
61 }
62 case 1:
63 {
64 timer1.Stop();
65 Timer_Status = 2;
66 button1.ForeColor = Color.Green; //字体显示绿色
67 button1.Text = "继续计时";
68 break;
69 }
70 case 2:
71 {
72 timer1.Start();
73 Timer_Status = 1;
74 button1.ForeColor = Color.Green; //字体显示绿色
75 button1.Text = "暂停计时";
76 break;
77 }
78 default:
79 {
80
81 break;
82 }
83 }
84
85
86 }
87
88 private void timer1_Tick(object sender, EventArgs e)
89 {
90 //定时器加1
91 Timer_Count++;
92 //更新剩余时间
93 textBox1.Text = (Timer_Value - Timer_Count).ToString() + " S";
94 //更新进度条
95 progressBar1.Value = Timer_Count;
96 if (Timer_Count == Timer_Value)
97 {
98 timer1.Stop();
99 Timer_Count = 0;
100 Timer_Status = 0;
101
102 button1.Text = "计时结束";
103 System.Media.SystemSounds.Asterisk.Play();//调用系统提示音
104 MessageBox.Show("定时时间到", "提示");
105 button1.ForeColor = Color.Black; //字体显示黑色
106 button1.Text = "开始定时";
107 textBox1.Text = "";
108 button2.Enabled = false;
109 comboBox1.Enabled = true;
110 comboBox2.Enabled = true;
111 comboBox1.Text = "0";
112 comboBox2.Text = "0";
113 //进度条归0
114 progressBar1.Value = 0;
115 }
116 }
117
118 private void button2_Click(object sender, EventArgs e)
119 {
120 timer1.Stop();
121 Timer_Count = 0;
122 Timer_Status = 0;
123 System.Media.SystemSounds.Asterisk.Play();//调用系统提示音
124 MessageBox.Show("定时停止", "提示");
125 button1.ForeColor = Color.Black; //字体显示黑色
126 button1.Text = "开始定时";
127
128 textBox1.Text = "";
129 button2.Enabled = false;
130 comboBox1.Enabled = true;
131 comboBox2.Enabled = true;
132 comboBox1.Text = "0";
133 comboBox2.Text = "0";
134
135 //进度条归0
136 progressBar1.Value = 0;
137 }
138
139
140 }
141 }
142