ProgressBar 类

表示 Windows 进度栏控件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<ComVisibleAttribute(True)> _
Public Class ProgressBar
    Inherits Control
用法
Dim instance As ProgressBar
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public class ProgressBar : Control
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
[ComVisibleAttribute(true)] 
public ref class ProgressBar : public Control
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class ProgressBar extends Control
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public class ProgressBar extends Control

备注

ProgressBar 控件以三种样式中的一种指示较长操作的进度:

  • 从左向右分步递增的分段块。

  • 从左向右填充的连续栏。

  • 以字幕方式在 ProgressBar 中滚动的块。

Style 属性确定显示的 ProgressBar 的样式。注意,ProgressBar 控件只能是水平方向的。有关如何创建垂直方向的 ProgressBar 的示例,请参见 ProgressBarRenderer 类。ProgressBar 控件通常在应用程序执行诸如复制文件或打印文档等任务时使用。如果没有视觉提示,应用程序的用户可能会认为应用程序不响应。通过在应用程序中使用 ProgressBar,可以警告用户应用程序正在执行冗长的任务且应用程序仍在响应。

MaximumMinimum 属性定义了两个值的范围用以表现任务的进度。Minimum 属性通常设置为值 0,Maximum 属性通常设置为指示任务完成的值。例如,若要正确显示复制一组文件时的进度,Maximum 属性应设置成要复制的文件的总数。

Value 属性表示应用程序在完成操作的过程中的进度。ProgressBar 显示的值仅仅是近似于 Value 属性的当前值。根据 ProgressBar 的大小,Value 属性确定何时显示下一个块或增加栏大小。

除了直接更改 Value 属性之外还有许多方式可以修改由 ProgressBar 显示的值。可以使用 Step 属性指定一个特定值用以逐次递增 Value 属性的值,然后调用 PerformStep 方法来使该值递增。若要更改增量值,可以使用 Increment 方法并指定一个用来递增 Value 属性的值。

提示

在 Windows XP Home Edition、Windows XP Professional、Windows Server 2003 以及更高版本上启用视觉样式时,对 ProgressBarForeColor 更改将不起作用。

示例

下面的代码示例使用 ProgressBar 控件来显示文件复制操作的进度。该示例使用 MinimumMaximum 属性指定 ProgressBar 的范围,它等效于要复制的文件数。代码还将 Step 属性用于 PerformStep 方法以在复制文件时增加 ProgressBar 的值。此示例要求已在 Form 中创建了名为 pBar1ProgressBar 控件,并且还创建了执行文件复制操作的名为 CopyFile 的方法(它返回一个布尔值,指示文件复制操作已成功完成)。此代码还要求已创建包含要复制的文件的字符串数组,并已将其传递给示例中定义的 CopyWithProgress 方法,而且要求该方法从 Form 中的另一个方法或事件进行调用。

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
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:
   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.set_Visible(true);
    // Set Minimum to 1 to represent the first file being copied.
    pBar1.set_Minimum(1);
    // Set Maximum to the total number of files to copy.
    pBar1.set_Maximum(fileNames.get_Length());
    // Set the initial value of the ProgressBar.
    pBar1.set_Value(1);
    // Set the Step property to a value of 1 to represent each file
    // being copied.
    pBar1.set_Step(1);
    // Loop through all files to copy.
    for (int x = 1; x <= fileNames.get_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();
        }
    }
} //CopyWithProgress

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
        System.Windows.Forms.ProgressBar

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ProgressBar 成员
System.Windows.Forms 命名空间