作者: littleboy 2022-09-12 11:53:21

进度条使用技巧

Application.DoEvents()方法

新建窗口类

image-20250212162104478

调用窗口

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++)
{
//命令操作
//......
//命令操作

//进度条+1
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++)
{
//进度条更新(数量+1)
ProgressBar_Number(i, filepath.Length);
if (Addcount != 0) { ProgressBar_Number(i, Addcount); }
}
//进度条完成
ProgressBar_finish();
}

参考