C#计时器

发布时间 2023-08-10 21:47:23作者: 蒙奇D派大星
namespace jishiqi
{
    public partial class Form1 : Form
    {
        int count;
        int time;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            for (int i = 1; i < 100; i++)//计时范围(0-99)
            {
                comboBox1.Items.Add(i.ToString() + "");//初始化下拉框内容“秒”前面加空格便于提取字符串位0开始两位
            }
            

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            count++;//记当前秒
            label2.Text = (time - count).ToString() + "";//显示剩余时间
            progressBar1.Value = count;//设置进度条进度
            if (count == time) ;
            timer1.Stop();
            System.Media.SystemSounds.Asterisk.Play();
            MessageBox.Show("时间到了","提示");
            count = 0;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = comboBox1.Text;//将下拉框的内容添加进变量str中
            string date = str.Substring(0, 2);
            time = Convert.ToInt16(date);//用于从0开始字符串两位数转换成Int32类型得到设定定时值
            progressBar1.Maximum = time ;//进度条最大值
            timer1.Start();//开始启动计时
        }
    }
}