天天看點

C#桌面應用程式--定時器

今天,開始學習C#,主要是依賴C#語言開發應用程式,特别是序列槽助手,以此來實作對單片機的序列槽資料讀取。

話不多說,今天根據學習視訊寫了下定時器的一個小小程式,菜鳥入坑。

以下是具體的實作代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 定時器
{
    public partial class Form1 : Form
    {
        int count=0;
        int timer;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int i;
            for (i = 1; i <= 100;i++)   //計數範圍:0~99
            {
                comboBox1.Items.Add(i.ToString()+" 秒");
            }
            label3.Text = "0秒";
            comboBox1.Text = "10 秒";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            count++;    //記錄目前的時間值
            label3.Text = (timer - count).ToString()+"秒";
            progressBar1.Value = count;
            if (count == timer)
            {
                timer1.Stop();  //時間到,停止計時
                System.Media.SystemSounds.Asterisk.Play();  //提示音
                MessageBox.Show("時間到!","提示");         //彈出提示框
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str;
            str = comboBox1.Text;      //得到字元串
            timer = Convert.ToInt16(str.Substring(0, 2));    //得到設定的資料值(整型)
            progressBar1.Maximum = timer;           //進度條最大數值
            timer1.Start();                 //定時器開始計時
        }



    }
}
           

程式界面很簡單,

C#桌面應用程式--定時器

就是實作對設定時間的簡單計時。