2010-04-14 67 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

相關問題