步骤任意时更新进度条

chw*_*hwi 0 c# floating-point int progress-bar

我正在编写一个C#应用程序,在其中处理文件中的行。该文件可能有2行,30,80,可能超过一百行。

这些行存储在列表中,因此我可以从获取行数myFileList.Count。进度条仅将int值用作参数,因此如果我的行号为50,则可以轻松地执行

int steps = 100/myFileList.Count 
progress += steps; 
updateProgressBar ( progress );
Run Code Online (Sandbox Code Playgroud)

但是,如果我的文件说61行:100/61 = 1,64,那么int steps它将等于1,并且进度条将停止在61%。如何正确执行此操作?

Seb*_*olm 5

在这里,我假设您正在使用System.Windows.Forms.ProgressBar

无需尝试计算进度百分比,只需将“ 最大”字段的值设置为行数即可。然后,您可以将值设置为您所在的行号,它将自动将其转换为合适的百分比。

// At some point when you start your computation:
pBar.Maximum = myFileList.Count;

// Whenever you want to update the progress:
pBar.Value = progress;

// Alternatively you can increment the progress by the number of lines processed
// since last update:
pBar.Increment(dLines);
Run Code Online (Sandbox Code Playgroud)