C#实现倒计时的功能

    xiaoxiao2025-05-03  16

     软件界面:

    软代码:

    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 comport { public partial class Form1 : Form { int count; int time; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)//下拉框的选择 { int i; for (i = 0; i < 100; i++) { comboBox1.Items.Add(i.ToString() + " 秒"); } comboBox1.Text = "5 秒";//下拉框选择的初始化 } private void button1_Click(object sender, EventArgs e)//按键按下后的操作逻辑 { string str = comboBox1.Text;//获取下拉框中选择的字符串内容 time = Convert.ToInt16(str.Substring(0,2));//将下拉菜单中的字符串内容转换成整形 progressBar1.Maximum = time;//进度条的最大值 timer1.Start();//开始定时器 } 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("时间到", "提示");//提示对话框 } } } }

    参考:B站杜洋工作室视频

    最新回复(0)