简体   繁体   English

C# - 更新进度条

[英]C# - Updating Progress Bar

I have an application that imports excel records into a user interface to read the data.我有一个将 excel 记录导入用户界面以读取数据的应用程序。 This import take a fair amount of time so I am trying to implement a progress bar for the user to see the progress.此导入需要相当长的时间,因此我正在尝试为用户实现一个进度条以查看进度。

First I did it in an external form, and everything worked fine.首先,我以外部形式进行了操作,一切正常。 Now i changed it to a progress bar on the loading form and suddenly it doesn't work.现在我将它更改为加载表单上的进度条,突然它不起作用。 All that I have done is chagne the form reference from my Class source sheet.我所做的只是从我的类源表中更改表单引用。 Here is the code that does the progress bar.这是执行进度条的代码。

The only change I made to that code is change to "OpenForm" as that is wher ethe new progress bar is.我对该代码所做的唯一更改是更改为“OpenForm”,因为这就是新进度条所在的位置。 I know this code will set the progress bar to 20 and not move after that.我知道这段代码会将进度条设置为 20,之后不会移动。 This was working on the form which had just the progress bar, however on this one it doesnt work.这是在只有进度条的表单上工作,但是在这个表单上它不起作用。 Am I missing something?我错过了什么吗?

        OpenForm.progressBar1.Minimum = 0;
        OpenForm.progressBar1.Maximum = 100;
        OpenForm.progressBar1.Step = 1 / 50;

        for (int row = 8; row <= 50; row++)
        { 
            IssueRef.Add(ExcelWksht1.Cells[row, 1].Value.ToString());
            Date.Add(ExcelWksht1.Cells[row, 4].Value.ToString());
            Status.Add(ExcelWksht1.Cells[row, 2].Value.ToString());
            Severity.Add(ExcelWksht1.Cells[row, 9].Value.ToString());
            Text.Add(ExcelWksht1.Cells[row, 3].Value.ToString());

            decimal ProgressVal = ( 10m /50m) * 100m;
            int Val = Convert.ToInt32(ProgressVal);

            OpenForm.progressBar1.Value = Val;
            OpenForm.progressBar1.Refresh();

        }
    for (int row = 8; row <= 50; row++)
        { 
            IssueRef.Add(ExcelWksht1.Cells[row, 1].Value.ToString());
            Date.Add(ExcelWksht1.Cells[row, 4].Value.ToString());
            Status.Add(ExcelWksht1.Cells[row, 2].Value.ToString());
            Severity.Add(ExcelWksht1.Cells[row, 9].Value.ToString());
            Text.Add(ExcelWksht1.Cells[row, 3].Value.ToString());

            decimal ProgressVal = ( 10m /50m) * 100m;
            int Val = Convert.ToInt32(ProgressVal);

            OpenForm.progressBar1.Value = Val;
            //OpenForm.progressBar1.Refresh(); remove this
//add 
Application.DoEvents();

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM