C#上位机-倒计时
文章目录
前言
通过上位机编写一个倒计时,作为入门学习的工程。
一、新建工程、工具箱界面
1.1 新建工程
如图所示:新建一个C#窗体应用程序 
1.2 工具箱

这里主要是本次工程用到的
1.2.1 groupBox
主要修改:Font、Text

1.2.2 label
主要修改:Font、Text
1.2.3 comboBox
主要修改:DropDownStyle,外观和功能

1.2.4 button
1.2.5 textBox
1.2.6 progressBar
1.2.7 timer
1.3 主界面

二、事件代码
1. 窗体装载事件
点击窗体----找到form1的事件----找到Load----点击,就会跳转到代码;
或者点击空白的地方

private void Form1_Load(object sender, EventArgs e)
{
byte i;
//分钟 秒钟组合初始化
for (i = 0; i < 60; i++)
{
comboBox1.Items.Add(i.ToString());
comboBox2.Items.Add(i.ToString());
}
//分钟秒钟组合默认值,只能程序加
comboBox1.Text = "0";
comboBox2.Text = "0";
}
2. 定时器单击事件
private void button1_Click(object sender, EventArgs e)
{
switch (Timer_Status)
{
//停止状态
case 0:
//获取定时时间
Timer_Value = Convert.ToUInt16(comboBox1.Text,10);//Convert把字符串转位数字,10代表十进制
Timer_Value *= 60;
Timer_Value += Convert.ToUInt16(comboBox2.Text, 10);
if (Timer_Value > 0)
{
textBox1.Text = Timer_Value.ToString() + " S";//显示设置时间+S
button1.Text = "暂停计时";
button1.ForeColor = Color.Green;//字体颜色
button2.Enabled = true;//可以按键
comboBox1.Enabled = false;//不可以按键
comboBox1.Enabled = false;//不可以按键
Timer_Status = 1;//更新状态,计时
//设置进度条
progressBar1.Value = 0;
progressBar1.Maximum = Timer_Value;
//启动计时
timer1.Start();
}
else
{
System.Media.SystemSounds.Exclamation.Play();//提示音
MessageBox.Show("定时时间不能为0!"," 警告");//弹窗,前面为内容,后面为标题
}
break;
//计时状态
case 1:
timer1.Stop();
Timer_Status = 2;
button1.Text = "继续计时";
break;
//暂停状态
case 2:
timer1.Start();
Timer_Status = 1;
button1.Text = "暂停计时";
break;
default:
break;
}
}
3. 停止按键事件
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
Timer_Count = 0;
Timer_Status = 0;//更新状态
button1.Text = "计时结束";
System.Media.SystemSounds.Beep.Play();//提示音
MessageBox.Show("停止计时", "提示");
button1.ForeColor = Color.Black;
button1.Text = " 开始计时";
textBox1.Text = "";
button2.Enabled = false;
comboBox1.Enabled = true;
comboBox1.Enabled = true;
comboBox1.Text = "0";
comboBox2.Text = "0";
progressBar1.Value = 0;
}
总结
工程已打包
2438

被折叠的 条评论
为什么被折叠?



