使用Winform空間編寫簡單的進度條小程序:小程序
所需控件:Lable 標籤 TextBox 文本框 progressBar 進度條控件 timer 定時器spa
下面是源碼及效果圖:日誌
1 /// <summary> 2 /// 進度條開始 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnBegin_Click(object sender, EventArgs e) 7 { 8 if (txtLenght.Text == "") 9 { 10 return; 11 } 12 progressBar1.Value = 0; 13 progressBar1.Minimum = 0; 14 progressBar1.Maximum = Convert.ToInt32(txtLenght.Text); 15 output("進度條開始運行"); 16 timer1.Enabled = true; 17 } 18 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 22 } 23 24 /// <summary> 25 /// 中止按鈕 26 /// </summary> 27 /// <param name="sender"></param> 28 /// <param name="e"></param> 29 private void btnStop_Click_1(object sender, EventArgs e) 30 { 31 output("進度條中止運行"); 32 timer1.Enabled = false; 33 progressBar1.Value = 0; 34 } 35 36 /// <summary> 37 /// 日誌記錄 38 /// </summary> 39 /// <param name="log"></param> 40 public void output(string log) 41 { 42 //若是日誌信息長度超過100行自動清空 43 if (txtJournal.GetLineFromCharIndex(txtJournal.Text.Length) > 150) 44 { 45 txtJournal.Text = ""; 46 } 47 //添加日誌 AppendText向文本框的當前文本追加文本 48 txtJournal.AppendText(DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss") + log + "\r\n"); 49 } 50 51 /// <summary> 52 /// 暫停按鈕 53 /// </summary> 54 /// <param name="sender"></param> 55 /// <param name="e"></param> 56 private void btnSuspend_Click(object sender, EventArgs e) 57 { 58 if (timer1.Enabled == true) 59 { 60 output("進度條暫停運行"); 61 btnSuspend.Text = "繼續"; 62 timer1.Enabled = false; 63 } 64 else 65 { 66 output("進度條繼續運行"); 67 btnSuspend.Text = "暫停"; 68 timer1.Enabled = true; 69 } 70 } 71 72 /// <summary> 73 /// 定時器 74 /// </summary> 75 /// <param name="sender"></param> 76 /// <param name="e"></param> 77 private void timer1_Tick(object sender, EventArgs e) 78 { 79 //若是進度條的當前值小於最大值,繼續運行 不然,結束 80 if (progressBar1.Value < progressBar1.Maximum) 81 { 82 progressBar1.Value++; 83 output("進度條進行中,{" + progressBar1.Value.ToString() + " / " + progressBar1.Maximum + "}"); 84 } 85 else 86 { 87 output("進度條已完成"); 88 timer1.Enabled = false; 89 } 90 }
效果圖展現:code