题目概述:
VS2017 Windows窗体应用(.NET Framework) 上面开发一个定时器。
编程:
namespace _003_7_27
{
public partial class Form1 : Form
{
int count;//用于定时器计数
int time;//存储设定的定时值
public Form1()
{
InitializeComponent();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
for(int i=1;i<100;i++)//计时范围
{
comboBox1.Items.Add(i.ToString()+" 秒");//初始化下拉框内容
}
}
private void timer1_Tick(object sender, EventArgs e)//定时器事件
{
count++;//记当前秒
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;
string data;
data = str.Substring(0, 2);
time = Convert.ToInt16(data);//Convert将一个字符转化成一个变量 第0位开始2个字符
progressBar1.Maximum = time;//进度条最大数值
timer1.Start();//计时器计时开始
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
}
}
上机实践: