在c#windows应用程序中使用Progressbar

时间:2010-04-14 04:22:22

标签: c#

用户将能够在本地计算机中搜索某些文档,并且我想在程序搜索期间显示用户,进度条。为了更清楚,我有foreach循环,我想绑定我的进度条以显示进度。我的foreach循环和进度应该同时工作。这可能吗?

1 个答案:

答案 0 :(得分:4)

来自ProgressBar Class

您可以尝试类似

的内容
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();
        }
    }
}

修改

对于较长时间运行的任务,您可能还会考虑使用BackgroundWorker Class