C#上位机-倒计时

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;
        }

总结

工程已打包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值