作者: littleboy
2022-09-12 11:53:21
进度条使用技巧
Application.DoEvents()方法
新建窗口类

调用窗口
1 2 3 4 5 6 7
| 进度条 form1 = new 进度条(); form1.Show();
form1.progressBar1.Value = 0; ; form1.progressBar1.Maximum = childrencomps.Length;
|
进度更新功能实现
1 2 3 4 5 6 7 8 9 10 11
| for (int i = 0; i < childrencomps.Length; i++) { form1.progressBar1.Value += 1; }
form1.progressBar1.Value = form1.progressBar1.Maximum; form1.Close();
|
作用
在程序点击运行时,弹窗进度条窗口。并开始显示进度
状态栏进度条
定义函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| private void ProgressBar_initialize() { toolStripProgressBar1.Value = 0; toolStripStatusLabel1.Text = "进度: "; toolStripProgressBar1.Maximum = filepath.Length + Addcount; }
private void ProgressBar_finish() { toolStripStatusLabel1.Text = "进度:完成"; toolStripProgressBar1.Value = toolStripProgressBar1.Maximum; }
private void ProgressBar_Number(int i, int length) { toolStripProgressBar1.Value = 0; toolStripProgressBar1.Value += 1; toolStripStatusLabel1.Text = "进度:" + (i + 1) + "/" + length; ; Application.DoEvents(); }
|
使用
1 2 3 4 5 6 7 8 9 10 11 12 13
| private void button2_Click(object sender, EventArgs e) { ProgressBar_initialize(); for (int i = 0; i < filepath.Length; i++) { ProgressBar_Number(i, filepath.Length); if (Addcount != 0) { ProgressBar_Number(i, Addcount); } } ProgressBar_finish(); }
|
参考