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 }
效果图展示: