共用方式為


如何:設定 Windows Form ProgressBar 控制項顯示的值

重要

ToolStripProgressBar 控制項會取代 ProgressBar 控制項並加入其他功能,不過您也可以選擇保留 ProgressBar 控制項,以提供回溯相容性及未來使用。

.NET Framework 提供了數種不同的方式讓您在 ProgressBar (部分機器翻譯) 控制項內顯示給定的值。 要選擇哪種方法取決於手邊的工作或您要解決的問題。 下表顯示您可以選擇的方法。

方法 描述
直接設定 ProgressBar (部分機器翻譯) 控制項的值。 這個方法適用於已知會涉及之測量項目總計的工作,例如從資料來源讀取記錄。 此外,如果您只需要對值進行一次或兩次的設定,這是可讓您這麼做的簡單方法。 最後,如果您需要減少進度列所顯示的值,請使用此流程。
以定值增加 ProgressBar (部分機器翻譯) 顯示。 這個方法適用於要顯示介於最小值和最大值之間的簡單計數時,例如經過的時間或已知總計中已處理的檔案數目。
以不定值增加 ProgressBar (部分機器翻譯) 顯示。 這個方法適用於當您需要以不同數量多次變更所顯示的值時。 舉例來說,在將一系列檔案寫入到磁碟時顯示所取用的硬碟空間數量。

若要設定進度列所顯示的值,最直接的方式是設定 Value (部分機器翻譯) 屬性。 您可以在設計階段或執行階段進行設定。

直接設定 ProgressBar 值

  1. 設定 ProgressBar (部分機器翻譯) 控制項的 Minimum (英文) 和 Maximum (英文) 值。

  2. 在程式碼中,將控制項的 Value (部分機器翻譯) 屬性設定為所建立最小值與最大值之間的整數值。

    注意

    如果您將 Value (部分機器翻譯) 屬性設定到由 Minimum (英文) 和 Maximum (英文) 屬性所建立的界限之外,該控制項會擲回 ArgumentException (部分機器翻譯) 例外狀況。

    下列程式碼範例說明如何直接設定 ProgressBar (部分機器翻譯) 值。 此程式碼會從資料來源讀取記錄,並在每次讀取資料記錄時更新進度列和標籤。 此範例會要求您的表單要有一個 Label (部分機器翻譯) 控制項、一個 ProgressBar (部分機器翻譯) 控制項以及一個運算列表,這個運算列表中有稱為 CustomerRow 的資料列,並有 FirstNameLastName 欄位。

    Public Sub CreateNewRecords()  
       ' Sets the progress bar's Maximum property to  
       ' the total number of records to be created.  
       ProgressBar1.Maximum = 20  
    
       ' Creates a new record in the dataset.  
       ' NOTE: The code below will not compile, it merely  
       ' illustrates how the progress bar would be used.  
       Dim anyRow As CustomerRow = DatasetName.ExistingTable.NewRow  
       anyRow.FirstName = "Stephen"  
       anyRow.LastName = "James"  
       ExistingTable.Rows.Add(anyRow)  
    
       ' Increases the value displayed by the progress bar.  
       ProgressBar1.Value += 1  
       ' Updates the label to show that a record was read.  
       Label1.Text = "Records Read = " & ProgressBar1.Value.ToString()  
    End Sub  
    
    public void createNewRecords()  
    {  
       // Sets the progress bar's Maximum property to  
       // the total number of records to be created.  
       progressBar1.Maximum = 20;  
    
       // Creates a new record in the dataset.  
       // NOTE: The code below will not compile, it merely  
       // illustrates how the progress bar would be used.  
       CustomerRow anyRow = DatasetName.ExistingTable.NewRow();  
       anyRow.FirstName = "Stephen";  
       anyRow.LastName = "James";  
       ExistingTable.Rows.Add(anyRow);  
    
       // Increases the value displayed by the progress bar.  
       progressBar1.Value += 1;  
       // Updates the label to show that a record was read.  
       label1.Text = "Records Read = " + progressBar1.Value.ToString();  
    }  
    

    如果您要顯示會以固定間隔推進的進度,您可以設定該值,然後呼叫會依該間隔增加 ProgressBar (部分機器翻譯) 控制項值的方法。 這適用於計時器和其他不會以整體百分比的形式來測量進度的案例。

以定值增加進度列

  1. 設定 ProgressBar (部分機器翻譯) 控制項的 Minimum (英文) 和 Maximum (英文) 值。

  2. 將控制項的 Step (部分機器翻譯) 屬性設定為整數,以代表用來增加進度列顯示值的數量。

  3. 呼叫 PerformStep (英文) 方法,以變更依 Step (部分機器翻譯) 屬性中所設定的數量來顯示的值。

    下列程式碼範例會說明進度列要如何在複製作業中維持檔案計數。

    在下列範例中,隨著每個檔案讀入到記憶體中,進度列和標籤會隨之更新以反映讀取的檔案總計。 此範例會要求您的表單具有 Label (部分機器翻譯) 控制項和 ProgressBar (部分機器翻譯) 控制項。

    Public Sub LoadFiles()  
       ' Sets the progress bar's minimum value to a number representing  
       ' no operations complete -- in this case, no files read.  
       ProgressBar1.Minimum = 0  
       ' Sets the progress bar's maximum value to a number representing  
       ' all operations complete -- in this case, all five files read.  
       ProgressBar1.Maximum = 5  
       ' Sets the Step property to amount to increase with each iteration.  
       ' In this case, it will increase by one with every file read.  
       ProgressBar1.Step = 1  
    
       ' Dimensions a counter variable.  
       Dim i As Integer  
       ' Uses a For...Next loop to iterate through the operations to be  
       ' completed. In this case, five files are to be copied into memory,  
       ' so the loop will execute 5 times.  
       For i = 0 To 4  
          ' Insert code to copy a file  
          ProgressBar1.PerformStep()  
          ' Update the label to show that a file was read.  
          Label1.Text = "# of Files Read = " & ProgressBar1.Value.ToString  
       Next i  
    End Sub  
    
    public void loadFiles()  
    {  
       // Sets the progress bar's minimum value to a number representing  
       // no operations complete -- in this case, no files read.  
       progressBar1.Minimum = 0;  
       // Sets the progress bar's maximum value to a number representing  
       // all operations complete -- in this case, all five files read.  
       progressBar1.Maximum = 5;  
       // Sets the Step property to amount to increase with each iteration.  
       // In this case, it will increase by one with every file read.  
       progressBar1.Step = 1;  
    
       // Uses a for loop to iterate through the operations to be  
       // completed. In this case, five files are to be copied into memory,  
       // so the loop will execute 5 times.  
       for (int i = 0; i <= 4; i++)  
       {  
          // Inserts code to copy a file  
          progressBar1.PerformStep();  
          // Updates the label to show that a file was read.  
          label1.Text = "# of Files Read = " + progressBar1.Value.ToString();  
       }  
    }  
    

    最後,您可以用每次都增加獨特數量的方式來增加進度列所顯示的值。 這適用於要追蹤一系列獨特作業 (例如將不同大小的檔案寫入到硬碟) 或以整體百分比的形式測量進度時。

以動態值增加進度列

  1. 設定 ProgressBar (部分機器翻譯) 控制項的 Minimum (英文) 和 Maximum (英文) 值。

  2. 呼叫 Increment (部分機器翻譯) 方法來變更依您指定的整數所顯示的值。

    下列程式碼範例會說明進度列要如何計算複製作業期間已使用多少磁碟空間。

    在下列範例中,隨著每個檔案寫入到硬碟中,進度列和標籤會隨之更新以反映可用的硬碟空間數量。 此範例會要求您的表單具有 Label (部分機器翻譯) 控制項和 ProgressBar (部分機器翻譯) 控制項。

    Public Sub ReadFiles()  
       ' Sets the progress bar's minimum value to a number
       ' representing the hard disk space before the files are read in.  
       ' You will most likely have to set this using a system call.  
       ' NOTE: The code below is meant to be an example and  
       ' will not compile.  
       ProgressBar1.Minimum = AvailableDiskSpace()  
       ' Sets the progress bar's maximum value to a number
       ' representing the total hard disk space.  
       ' You will most likely have to set this using a system call.  
       ' NOTE: The code below is meant to be an example
       ' and will not compile.  
       ProgressBar1.Maximum = TotalDiskSpace()  
    
       ' Dimension a counter variable.  
       Dim i As Integer  
       ' Uses a For...Next loop to iterate through the operations to be  
       ' completed. In this case, five files are to be written to the disk,  
       ' so it will execute the loop 5 times.  
       For i = 1 To 5  
          ' Insert code to read a file into memory and update file size.  
          ' Increases the progress bar's value based on the size of
          ' the file currently being written.  
          ProgressBar1.Increment(FileSize)  
          ' Updates the label to show available drive space.  
          Label1.Text = "Current Disk Space Used = " &_
          ProgressBar1.Value.ToString()  
       Next i  
    End Sub  
    
    public void readFiles()  
    {  
       // Sets the progress bar's minimum value to a number
       // representing the hard disk space before the files are read in.  
       // You will most likely have to set this using a system call.  
       // NOTE: The code below is meant to be an example and
       // will not compile.  
       progressBar1.Minimum = AvailableDiskSpace();  
       // Sets the progress bar's maximum value to a number
       // representing the total hard disk space.  
       // You will most likely have to set this using a system call.  
       // NOTE: The code below is meant to be an example
       // and will not compile.  
       progressBar1.Maximum = TotalDiskSpace();  
    
       // Uses a for loop to iterate through the operations to be  
       // completed. In this case, five files are to be written  
       // to the disk, so it will execute the loop 5 times.  
       for (int i = 1; i<= 5; i++)  
       {  
          // Insert code to read a file into memory and update file size.  
          // Increases the progress bar's value based on the size of
          // the file currently being written.  
          progressBar1.Increment(FileSize);  
          // Updates the label to show available drive space.  
          label1.Text = "Current Disk Space Used = " + progressBar1.Value.ToString();  
       }  
    }  
    

另請參閱