如何控制进度条的增量。 [英] How to control the increment of progress bar.

查看:87
本文介绍了如何控制进度条的增量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:



  private   void  button1_Click( object  sender,EventArgs e)
{
backgroundWorker1.RunWorkerAsync( );
}


私有 void backgroundWorker1_ProgressChanged(< span class =code-keyword> object sender,ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}

private void backgroundWorker1_RunWorkerCompleted( object sender,RunWorkerCompletedEventArgs e)
{
MessageBox.Show( 完整);
}


私有 void backgroundWorker1_DoWork(< span class =code-keyword> object
sender,DoWorkEventArgs e)
{

int [] arr = new int [ 20 ];
for int i = 1 ; i < = 20 ; i ++)
{
arr [我] =我;
Thread.Sleep( 100 );
backgroundWorker1.ReportProgress(i);
}
}



只有当控件退出for循环时,才会将进度条增加到20%。我希望在for循环按比例完成20个循环时,进度条增加到100%。例如/ - 在第一个循环完成后,进度条应增加5%(如(1/20)* 100))。请帮忙。

解决方案

正如您可能从参数名称猜测的那样, ReportProgress 方法的第一个参数是从 ProgressChangedEventArgs 类的 ProgressPercentage 属性返回的百分比。



您只传递1到20之间的值,这就是进度条永远不会超过20%的原因。


将值转换为百分比在将它们传递给 ReportProgress 方法之前:

 backgroundWorker1.ReportProgress(i *  <5 /跨度>); 


I'm using the following code:

private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }


        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Complete");
        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            int[] arr = new int[20];
            for (int i = 1; i <= 20; i++)
            {
                arr[i] = i;
                Thread.Sleep(100);
                backgroundWorker1.ReportProgress(i);
            }
        }


This only increments the progress bar only to 20% by the time the control exits the for loop. I want the progress bar to increment to 100% by the time the for loop completes the 20 loops proportionally. eg/- the progress bar should increment by 5% (as in (1/20)*100)) after the first loop is complete . Please help.

解决方案

As you could probably guess from the parameter name, the first argument to the ReportProgress method is the percentage returned from the ProgressPercentage property of the ProgressChangedEventArgs class.

You are only passing in values between 1 and 20, which is why the progress bar never gets above 20%.

Convert the values to a percentage before passing them to the ReportProgress method:

backgroundWorker1.ReportProgress(i * 5);


这篇关于如何控制进度条的增量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆