ProgressBar.PerformStep 方法

定義

根據 Step 屬性所設定的量,在進度列上從目前位置前進到下一個位置。Advances the current position of the progress bar by the amount of the Step property.

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

例外狀況

範例

下列程式碼範例會使用 ProgressBar 控制項來顯示檔案複製作業的進度。The following code example uses a ProgressBar control to display the progress of a file copy operation. 此範例會使用 MinimumMaximum 屬性來指定的範圍,其 ProgressBar 相當於要複製的檔案數目。The example uses the Minimum and Maximum properties to specify a range for the ProgressBar that is equivalent to the number of files to copy. 程式碼也會搭配 Step 方法使用屬性, PerformStep 以便在複製檔案時遞增的值 ProgressBarThe code also uses the Step property with the PerformStep method to increment the value of the ProgressBar as a file is copied. 這個範例會要求您建立一個 ProgressBar 呼叫的控制項 pBar1 ,該控制項是在中建立的 Form ,而且有一個稱為 (的方法,它會傳回 CopyFile 布林值,指出檔案複製作業已順利完成) 執行檔案複製作業。This example requires that you have a ProgressBar control created called pBar1 that is created within a Form, and that there is a method created called CopyFile (that returns a Boolean value indicating the file copy operation was completed successfully) that performs the file copy operation. 程式碼也需要建立包含要複製之檔案的字串陣列,並傳遞至 CopyWithProgress 範例中所定義的方法,並且從中的另一個方法或事件呼叫該方法 FormThe code also requires that an array of strings containing the files to copy is created and passed to the CopyWithProgress method defined in the example, and that the method is called from another method or event in the 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方法會以屬性指定的數量來遞增進度列的值 StepThe PerformStep method increments the value of the progress bar by the amount specified by the Step property. 您可以使用 Step 屬性來指定作業中每個已完成工作的數量變更進度列的值。You can use the Step property to specify the amount that each completed task in an operation changes the value of the progress bar. 例如,如果您要複製一組檔案,您可能會想要將屬性的值設定 Step 為1,並將屬性的值設定為 Maximum 要複製的檔案總數。For example, if you are copying a group of files, you might want to set the value of the Step property to 1 and the value of the Maximum property to the total number of files to copy. 複製每個檔案時,您可以呼叫 PerformStep 方法,以屬性的值遞增進度列 StepWhen each file is copied, you can call the PerformStep method to increment the progress bar by the value of the Step property. 如果您想要更有彈性地控制進度列的值,您可以使用 Increment 方法或直接設定屬性的值 ValueIf you want to have more flexible control of the value of the progress bar, you can use the Increment method or set the value of the Value property directly.

Value屬性會指定目前的位置 ProgressBarThe Value property specifies the current position of the ProgressBar. 如果在呼叫方法之後 PerformStepValue 屬性大於屬性的值 Maximum ,則 Value 屬性會維持在屬性的值 MaximumIf, after calling the PerformStep method, the Value property is greater than the value of the Maximum property, the Value property remains at the value of the Maximum property. 如果在呼叫方法時, PerformStep 使用指定的負值來呼叫方法 Step ,則屬性會小於屬性的值,屬性 Value 會維持在屬性的 Minimum ValueMinimumIf, after calling the PerformStep method with a negative value specified in Step, the Value property is less than the value of the Minimum property, the Value property remains at the value of the Minimum property.

因為 ProgressBar 其樣式設定為 Marquee 顯示持續捲軸(而非其)的物件 Value ,所以不需要呼叫, PerformStep 並且會引發 InvalidOperationExceptionBecause a ProgressBar object whose style is set to Marquee displays a continuously scrolling bar instead of its Value, calling PerformStep is unnecessary and will raise an InvalidOperationException.

適用於

另請參閱