重要
ToolStripProgressBar 控件替换并添加 ProgressBar 控件的功能;但是,如果选择,则保留 ProgressBar 控件以实现后向兼容性和将来使用。
.NET Framework 提供了多种不同的方法来在控件中 ProgressBar 显示给定值。 选择哪种方法取决于手头的任务或要解决的问题。 下表显示了可以选择的方法。
方法 | DESCRIPTION |
---|---|
将ProgressBar控件的值直接设置。 | 对于那些您已知所涉及项的总数的任务(例如从数据源读取记录),此方法非常有用。 此外,如果只需要设置一次或两次值,这是一种简单的方法。 最后,如果需要减少进度栏显示的值,请使用此过程。 |
ProgressBar按固定值增加显示。 | 当在最小值和最大值之间显示简单计数时(例如已用时间或在已知总数中的已处理文件数),此方法非常有用。 |
将ProgressBar显示按一个变化的值进行调整。 | 如果需要在不同数量中多次更改显示的值,此方法非常有用。 例如,在将一系列文件写入磁盘时消耗的硬盘空间量。 |
设置进度栏显示的值的最直接方法是设置 Value 属性。 这可以在设计时或运行时完成。
直接设置 ProgressBar 值
设置ProgressBar控件的Minimum和Maximum值。
在代码中,将控件 Value 的属性设置为已建立的最小值和最大值之间的整数值。
注释
如果将Value属性设置在Minimum和Maximum属性所建立的边界之外,控件将引发ArgumentException异常。
下面的代码示例演示如何直接设置 ProgressBar 值。 代码从数据源读取记录,并在每次读取数据记录时更新进度栏和标签。 此示例要求窗体具有一个Label控件、一个ProgressBar控件以及一个数据表,其中的行
CustomerRow
具有字段FirstName
和LastName
。Public Sub CreateNewRecords() ' Sets the progress bar's Maximum property to ' the total number of records to be created. ProgressBar1.Maximum = 20 ' Creates a new record in the dataset. ' NOTE: The code below will not compile, it merely ' illustrates how the progress bar would be used. Dim anyRow As CustomerRow = DatasetName.ExistingTable.NewRow anyRow.FirstName = "Stephen" anyRow.LastName = "James" ExistingTable.Rows.Add(anyRow) ' Increases the value displayed by the progress bar. ProgressBar1.Value += 1 ' Updates the label to show that a record was read. Label1.Text = "Records Read = " & ProgressBar1.Value.ToString() End Sub
public void createNewRecords() { // Sets the progress bar's Maximum property to // the total number of records to be created. progressBar1.Maximum = 20; // Creates a new record in the dataset. // NOTE: The code below will not compile, it merely // illustrates how the progress bar would be used. CustomerRow anyRow = DatasetName.ExistingTable.NewRow(); anyRow.FirstName = "Stephen"; anyRow.LastName = "James"; ExistingTable.Rows.Add(anyRow); // Increases the value displayed by the progress bar. progressBar1.Value += 1; // Updates the label to show that a record was read. label1.Text = "Records Read = " + progressBar1.Value.ToString(); }
如果显示按固定间隔继续的进度,可以设置该值,然后调用一个方法,该方法按该间隔增加 ProgressBar 控件的值。 这对于计时器和其他未将进度度量为整体百分比的情况非常有用。
按固定值增加进度条
设置ProgressBar控件的Minimum和Maximum值。
将控件 Step 的属性设置为一个整数,表示增加进度栏的显示值的数量。
调用PerformStep方法以根据Step属性中设置的值来更改显示的值。
下面的代码示例演示了进度条如何用于记录复制操作中文件的数量。
在以下示例中,由于每个文件都读入内存中,进度栏和标签会更新,以反映读取的文件总数。 此示例要求窗体具有控件 Label 和 ProgressBar 控件。
Public Sub LoadFiles() ' Sets the progress bar's minimum value to a number representing ' no operations complete -- in this case, no files read. ProgressBar1.Minimum = 0 ' Sets the progress bar's maximum value to a number representing ' all operations complete -- in this case, all five files read. ProgressBar1.Maximum = 5 ' Sets the Step property to amount to increase with each iteration. ' In this case, it will increase by one with every file read. ProgressBar1.Step = 1 ' Dimensions a counter variable. Dim i As Integer ' Uses a For...Next loop to iterate through the operations to be ' completed. In this case, five files are to be copied into memory, ' so the loop will execute 5 times. For i = 0 To 4 ' Insert code to copy a file ProgressBar1.PerformStep() ' Update the label to show that a file was read. Label1.Text = "# of Files Read = " & ProgressBar1.Value.ToString Next i End Sub
public void loadFiles() { // Sets the progress bar's minimum value to a number representing // no operations complete -- in this case, no files read. progressBar1.Minimum = 0; // Sets the progress bar's maximum value to a number representing // all operations complete -- in this case, all five files read. progressBar1.Maximum = 5; // Sets the Step property to amount to increase with each iteration. // In this case, it will increase by one with every file read. progressBar1.Step = 1; // Uses a for loop to iterate through the operations to be // completed. In this case, five files are to be copied into memory, // so the loop will execute 5 times. for (int i = 0; i <= 4; i++) { // Inserts code to copy a file progressBar1.PerformStep(); // Updates the label to show that a file was read. label1.Text = "# of Files Read = " + progressBar1.Value.ToString(); } }
最后,可以增加进度条显示的值,以便每次增加都是唯一的量。 在跟踪一系列唯一操作时(例如将不同大小的文件写入硬盘或将进度测量为整体的百分比),这非常有用。
用动态值增加进度条
设置ProgressBar控件的Minimum和Maximum值。
调用Increment方法来更改由您指定的整数所显示的值。
下面的代码示例演示了进度栏如何计算复制作期间已使用的磁盘空间量。
在以下示例中,由于每个文件都写入硬盘,因此进度栏和标签会更新,以反映可用硬盘空间量。 此示例要求窗体具有控件 Label 和 ProgressBar 控件。
Public Sub ReadFiles() ' Sets the progress bar's minimum value to a number ' representing the hard disk space before the files are read in. ' You will most likely have to set this using a system call. ' NOTE: The code below is meant to be an example and ' will not compile. ProgressBar1.Minimum = AvailableDiskSpace() ' Sets the progress bar's maximum value to a number ' representing the total hard disk space. ' You will most likely have to set this using a system call. ' NOTE: The code below is meant to be an example ' and will not compile. ProgressBar1.Maximum = TotalDiskSpace() ' Dimension a counter variable. Dim i As Integer ' Uses a For...Next loop to iterate through the operations to be ' completed. In this case, five files are to be written to the disk, ' so it will execute the loop 5 times. For i = 1 To 5 ' Insert code to read a file into memory and update file size. ' Increases the progress bar's value based on the size of ' the file currently being written. ProgressBar1.Increment(FileSize) ' Updates the label to show available drive space. Label1.Text = "Current Disk Space Used = " &_ ProgressBar1.Value.ToString() Next i End Sub
public void readFiles() { // Sets the progress bar's minimum value to a number // representing the hard disk space before the files are read in. // You will most likely have to set this using a system call. // NOTE: The code below is meant to be an example and // will not compile. progressBar1.Minimum = AvailableDiskSpace(); // Sets the progress bar's maximum value to a number // representing the total hard disk space. // You will most likely have to set this using a system call. // NOTE: The code below is meant to be an example // and will not compile. progressBar1.Maximum = TotalDiskSpace(); // Uses a for loop to iterate through the operations to be // completed. In this case, five files are to be written // to the disk, so it will execute the loop 5 times. for (int i = 1; i<= 5; i++) { // Insert code to read a file into memory and update file size. // Increases the progress bar's value based on the size of // the file currently being written. progressBar1.Increment(FileSize); // Updates the label to show available drive space. label1.Text = "Current Disk Space Used = " + progressBar1.Value.ToString(); } }