环境:VS
语言:C#
创建一个模版:
一、窗口设计
(一)使用工具箱简单设置图形化界面:
label、Botton、progressBar、ComboBox、Timer
(二)右键属性设置初始值和界面:
ComboBox属性设置为DropDownList,即可修改的
Timer初值修改为1000(ms)
二、代码编写
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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Timer
{
public partial class Form1 : Form
{
int count;/*计数器*/
int time;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for(int i = 0; i < 100; i ++)
{
/*将i转换成String类型,将该类型使用Add方法添加为下拉列表的选项*/
comboBox1.Items.Add(i.ToString() + " 秒");
}
/*初始化*/
comboBox1.Text = "0 秒";
label3.Text = "0 秒";
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
count++;
label3.Text = (time - count).ToString()+ " 秒";/*显示剩余时间*/
progressBar1.Value = count;/*设置进度条的进度*/
/*当时间到了*/
if(time == count)
{
timer1.Stop();/*定时器停止*/
System.Media.SystemSounds.Asterisk.Play();/*播放系统提示音*/
MessageBox.Show("时间到了" ,"提示");/*提示框,内容为"时间到了"*/
}
}
private void button1_Click(object sender, EventArgs e)
{
string str = comboBox1.Text;/*直接获取comboBox1的内容*/
/*Substring(a,b)从下标第a位开始取字符串前b位*/
time = Convert.ToInt32(str.Substring(0, 2));/*comboBox1的String转换成int*/
progressBar1.Maximum = time;/*进度条最大值*/
timer1.Start();/*按键后启动定时器*/
}
}
}