1. 程式人生 > >C# 定時器

C# 定時器

part 初始 進度 .com using ota time lin 圖片

定時器界面:

技術分享圖片

定時器代碼:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        
int count = 0; int total_time; public Form1() { InitializeComponent(); } // 該函數在窗體加載時執行 private void Form1_Load(object sender, EventArgs e) { // 為combobox1添加時間列表 for (int i = 1; i < 100; i++) { comboBox1.Items.Add(i.ToString()
+ ""); // 添加每一列 }
comboBox1.Text = "1 秒"; // 設置初始值 }
private void label1_Click(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } // 計時器函數 private void timer1_Tick(object
sender, EventArgs e) { ++count; // 自增1 int remaining_time = total_time - count; // 計算剩余時間 label3.Text = remaining_time.ToString() + ""; // 顯示剩余時間 progressBar1.Value = count; // 進度條顯示的進度 // 如果剩余時間為0,執行 if (remaining_time == 0) { timer1.Stop(); // 停止計時 System.Media.SystemSounds.Asterisk.Play(); // 播放系統聲音 MessageBox.Show("計時結束", "提示"); // 彈出框進行提示 } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { // } // 點擊button1開始執行該函數 private void button1_Click(object sender, EventArgs e) { string str_time = comboBox1.Text; // 獲取combobox1選中的時間 total_time = Convert.ToInt16(str_time.Substring(0, 2)); // 將選中的時間進行轉換為整形。Substring(開始下標,截取長度):為截取字符串 progressBar1.Maximum = total_time; // 設置滾動條顯示的最大時間 timer1.Start(); // 開啟計時 } } }

C# 定時器