ProgressBar 类

来源:互联网 发布:sql2008数据库下载 编辑:程序博客网 时间:2024/05/01 23:53

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

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

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

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

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

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

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

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

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();        }    }}