ProgressBar.PerformStep 方法

定义

按照 Step 属性的数量增加进度栏的当前位置。

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

例外

示例

下面的代码示例使用 ProgressBar 控件显示文件复制操作的进度。 该示例使用 MinimumMaximum 属性为等效于要复制的文件数指定范围 ProgressBar 。 该代码还使用 Step 具有方法的属性 PerformStep 来递增复制文件时的值 ProgressBar 。 此示例要求你创建pBar1一个名为在一Form个中创建的控件,并且有一个ProgressBar称为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

适用于

另请参阅