C# 为什么我的进度条在加载表单时没有增加?

C# 为什么我的进度条在加载表单时没有增加?,c#,C#,我有我将在下面展示的表格。我希望进度条在onLoad事件上缓慢运行,以测试它是否正常工作 我是否应该使用这段代码循环遍历进度条值 问题:为什么在onLoad()时进度条不增加? progressBar1.Value = 0; //start at zero value int n = 100; double progress = 0; //This loop should increment the progress b

我有我将在下面展示的表格。我希望进度条在onLoad事件上缓慢运行,以测试它是否正常工作

我是否应该使用这段代码循环遍历进度条值

问题:为什么在onLoad()时进度条不增加?

         progressBar1.Value = 0; //start at zero value
         int n = 100;

         double progress = 0;

         //This loop should increment the progress bar.
         for (int i = 1; i <= n; i++)
         {
             if (progress <= 100)
             {
                 progressBar1.Value = (int)progress;
             }
         }
progressBar1.Value=0//从零开始
int n=100;
双进度=0;
//这个循环应该增加进度条。

对于(int i=1;i,因为您从不增加进度值。您只需将其设置为
0
,然后反复将其分配给progressBar1的值。

存在多个问题:

  • progress
    的值永远不会更改
  • 您在一个线程上完成了这一切,因此在方法完成之前,该条不会更新
  • 您只是从1递增到100,中间没有工作,因此增量更新速度太快,您无法看到条形图的更改

  • 您没有递增
    进度
    变量。请尝试以下操作:

             for (int progress = 0; i <= n; progress++)
             {
                     progressBar1.Value = progress;
             }
    
    for(int progress=0;i
    
             for (int progress = 0; i <= n; progress++)
             {
                     progressBar1.Value = progress;
             }