1

如何在嵌套的 foreach 循环中正确使用进度条?因为我是这样做的,而我在 foreach 循环进程中的进程只执行到我正在比较的第二个文件。

int count = d1.GetFiles(fd.fileType, SearchOption.AllDirectories).Length;
int current = 0;
foreach(...)
{
    foreach(...)
    {
      //process
    }
current++;
pbSearch.Value = current / count * 30 + 70;
label1.Text = pbSearch.Value.ToString();
}
4

1 回答 1

1

根据您的两个 foreach 循环,您可以考虑使用两个进度条或重新计算最大值,看看这个MSDN示例:

    private void CopyWithProgress(string[] filenames)
    {
        // Display the ProgressBar control.
        pBar1.Visible = true;
        // Set Minimum to 1 to represent the first file being copied.
        pBar1.Minimum = 1;
        // Set Maximum to the total number of files to copy.
        pBar1.Maximum = filenames.Length;
        // Set the initial value of the ProgressBar.
        pBar1.Value = 1;
        // Set the Step property to a value of 1 to represent each file being copied.
        pBar1.Step = 1;

        // Loop through all files to copy. 
        for (int x = 1; x <= filenames.Length; x++)
        {
            // Copy the file and increment the ProgressBar if successful. 
            if(CopyFile(filenames[x-1]) == true)
            {
                // Perform the increment on the ProgressBar.
                pBar1.PerformStep();
            }
        }
    }
于 2013-11-08T07:32:47.077 回答