1. 程式人生 > >Winfrom 簡單的進度條小程式

Winfrom 簡單的進度條小程式

使用Winform空間編寫簡單的進度條小程式:

         所需控制元件:Lable 標籤  TextBox  文字框  progressBar  進度條控制元件  timer 定時器

          下面是原始碼及效果圖:

 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 }

 

       效果圖展示: