ProgressBar.PerformStep 方法

定義

根據 Step 屬性所設定的量,在進度列上從目前位置前進到下一個位置。

public:
 void PerformStep();
public void PerformStep ();
member this.PerformStep : unit -> unit
Public Sub PerformStep ()

例外狀況

範例

下列程式碼範例會使用 ProgressBar 控制項來顯示檔案複製作業的進度。 此範例會使用 MinimumMaximum 屬性,指定要 ProgressBar 複製之 檔案數目的 範圍。 程式碼也會使用 Step 屬性搭配 PerformStep 方法,以複製檔案時遞增 的值 ProgressBar 。 此範例會要求您建立 ProgressBar 名為 pBar1Form 控制項,該控制項是在 內建立,而且有一個稱為 CopyFile (建立的方法,會傳回布林值,指出檔案複製作業已順利完成,) 執行檔案複製作業。 程式碼也需要建立包含要複製之檔案的字串陣列,並傳遞至 CopyWithProgress 範例中定義的方法,並且從 中的另一個方法或事件 Form 呼叫 方法。

private:
   void CopyWithProgress( array<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();
         }
      }
   }
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();
        }
    }
}
Private Sub CopyWithProgress(ByVal ParamArray filenames As String())
    ' 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.
    Dim x As Integer
    for x = 1 To filenames.Length - 1
        ' Copy the file and increment the ProgressBar if successful.
        If CopyFile(filenames(x - 1)) = True Then
            ' Perform the increment on the ProgressBar.
            pBar1.PerformStep()
        End If
    Next x
End Sub

備註

方法 PerformStep 會將進度列的值遞增屬性所 Step 指定的數量。 您可以使用 Step 屬性來指定作業中每個已完成工作變更進度列值的數量。 例如,如果您要複製一組檔案,您可能想要將 屬性的值 Step 設定為 1,並將 屬性的值 Maximum 設定為要複製的檔案總數。 複製每個檔案時,您可以呼叫 PerformStep 方法,藉由 屬性的值 Step 遞增進度列。 如果您想要更彈性地控制進度列的值,您可以使用 Increment 方法或直接設定 屬性的值 Value

屬性 Value 會指定 的 ProgressBar 目前位置。 如果在呼叫 PerformStep 方法之後, Value 屬性大於 屬性的值 Maximum ,則 Value 屬性會維持在 屬性的值 Maximum 。 如果在呼叫 PerformStep 方法時指定了負值 Step ,則 Value 屬性小於 屬性的值 Minimum ,則 Value 屬性會維持在 屬性的值 Minimum

ProgressBar因為其樣式設定為 Marquee 顯示連續捲軸而非其 Value 的物件,所以呼叫 PerformStep 是不必要的,而且會引發 InvalidOperationException

適用於

另請參閱