C#学习记录——实例练习:倒计时器
生活随笔
收集整理的這篇文章主要介紹了
C#学习记录——实例练习:倒计时器
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、實(shí)現(xiàn)功能簡(jiǎn)介
本小練習(xí)是在學(xué)習(xí)《C#上位機(jī)開(kāi)發(fā)串口通信編程》課程時(shí)的一個(gè)倒計(jì)時(shí)器的小樣例,本樣例可以實(shí)現(xiàn)設(shè)置倒計(jì)時(shí)時(shí)長(zhǎng)1-99秒,然后點(diǎn)擊開(kāi)始計(jì)時(shí)按鈕,然后倒計(jì)時(shí)顯示剩余時(shí)間,以及時(shí)長(zhǎng)流動(dòng)的進(jìn)度條,當(dāng)達(dá)到倒計(jì)時(shí)時(shí)間時(shí)會(huì)彈出“時(shí)間到了”提示框。在此記錄下來(lái),方便復(fù)盤的同時(shí)和感興趣的同學(xué)一起學(xué)習(xí)。
2、 界面設(shè)計(jì)
2.1、界面樣式
1、主界面
2、提示框
2.2、添加控件及屬性設(shè)置
| Size | 400,150 | 設(shè)置窗口尺寸 | |
| Text | 倒計(jì)時(shí)器 | 設(shè)置名稱 | |
| comboBox | Text | 下拉菜單 | |
| comboBox | DropDownStyle | DropDownList | 設(shè)置成不可改 |
| Lable1 | Text | 時(shí)長(zhǎng): | 標(biāo)簽顯示名稱 |
| Lable2 | Text | 剩余時(shí)間: | 標(biāo)簽名稱 |
| Lable3 | Text | 1秒 | 標(biāo)簽顯示 |
| Button | (name) | btnOpenCom | 設(shè)置name |
| Button | Text | 開(kāi)始計(jì)時(shí) | 設(shè)置按鍵名稱 |
| ProgressBar | Text | 填充進(jìn)度條 | |
| timer1 | 添加定時(shí)器 | ||
| timer1 | Interval | 1000 | 修改事件的頻率 |
3、代碼實(shí)現(xiàn)與分析
3.1、倒計(jì)時(shí)
int i; for(i=1;i<100;i++) { comboBox1.Items.Add(i.ToString()+“秒”); }3.2、Timer的觸發(fā)事件
timer1.Start();
布局界面
完整代碼如下:
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 L021_countdown {public partial class Form1 : Form{int count;//用于定時(shí)器計(jì)數(shù)int time;//存儲(chǔ)設(shè)定的定時(shí)值public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){int i;for (i = 1; i < 100; i++)//計(jì)數(shù)范圍(1-99){//初始化下下拉內(nèi)容(秒字前加一個(gè)空格便于程序)comboBox1.Items.Add(i.ToString()+" 秒");}label3.Text = "";}//定時(shí)器事件private void timer1_Tick(object sender, EventArgs e){//記當(dāng)前秒count++;label3.Text = (time - count).ToString() + "秒";//顯示剩余時(shí)間progressBar1.Value = count;//設(shè)置進(jìn)度條進(jìn)度if(count==time){timer1.Stop();//時(shí)間到,停止計(jì)時(shí)System.Media.SystemSounds.Asterisk.Play();//提示音MessageBox.Show("時(shí)間到了!", "提示");//彈出提示框}}//開(kāi)始計(jì)時(shí)按鈕事件private void button1_Click(object sender, EventArgs e){string str = comboBox1.Text;//將下拉框內(nèi)容添加到一個(gè)變量中time = Convert.ToInt32(str.Substring(0, 2));//得到設(shè)定定時(shí)值()整型progressBar1.Maximum = time;//進(jìn)度條最大數(shù)值timer1.Start();//開(kāi)始計(jì)時(shí) }} }4、源碼連接:
https://download.csdn.net/download/u013097500/86777148
總結(jié)
以上是生活随笔為你收集整理的C#学习记录——实例练习:倒计时器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python倒计时器弹出计时窗口,pyt
- 下一篇: C#开发BIMFACE系列9 服务端AP