2010-04-14 76 views
2

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

回答

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

+0

@astander,感谢您的代码。这对我帮助很大。我在按钮单击事件中使用此代码。进度条在运行时不起作用。 – Anuya 2010-04-14 06:28:32

相关问题