在Vs里写一个C#倒计时源码注释

2 篇文章 0 订阅
1 篇文章 0 订阅

在Vs里写一个C#倒计时

包含 开始计时,暂停计时,继续计时,停止计时四个功能。计数范围0-199秒(可改)。

有1个下拉**comboBox控件,
4个
button按钮控件,
一个
timer定时器,
一个进度条
progressBar**控件

软件页面:
在这里插入图片描述

以下是代码

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;

namespace timer
{
    public partial class Form1 : Form
    {
        int count;//用于定时器定时计数
        int time;//存储设定的定时值
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int i;
            for(i = 1;i < 200; i++)//计时范围(0-199)
            {
                comboBox1.Items.Add(i.ToString() + " 秒");//初始化下拉框内容 //ToString转换格式为string
                                                          //属性的Items中也可设置
                                                          //有空格便于秒数的提取,不会提取到”秒“字
            }
            label3.Text = "";//剩余时间清空
            comboBox1.Text = "1 秒";//剩余时间清空
        }
        private void timer1_Tick(object sender, EventArgs e) // 定时器事件 类似中断  中断时间在可视化属性中Interval修改 
        {
            count++; //计当前秒
            label3.Text = (time - count).ToString() + "秒";//显示剩余时间  不需要提取,可以不加空格
            progressBar1.Value = count;//设置进度条进度
            if(count == time)
            {
                //MessageBox.Show("计时结束!", "提示");//如果放在这,定时器继续执行,count继续+1,超过进度条范围报错 
                timer1.Stop();//时间到,停止计时
                System.Media.SystemSounds.Asterisk.Play();//提示音 Play播放才有声音 此语句有延时,所以先timerStop
                MessageBox.Show("计时结束!", "提示");//弹出提示框   show("内容","标题")
                                                      //要放在最后 
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = comboBox1.Text;//得到下拉框选定的值
            string data = str.Substring(0, 2);
            time = Convert.ToInt32(data);//得到设定的值(整形) 从第0位开始查,查2位
            progressBar1.Maximum = time;
            timer1.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            System.Media.SystemSounds.Beep.Play();//提示音 Play播放才有声音
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Stop();//时间到,停止计时
            label3.Text = "";//剩余时间清空
            progressBar1.Value = 0;
            count = 0;
            System.Media.SystemSounds.Asterisk.Play();

        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }
    }
}

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值