在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)
{
}
}
}