设置 Windows 窗体 ProgressBar 控件显示的值
<!--NONSCROLLING BANNER END-->
<!-- Topic Status -->
.NET Framework 使您可以使用多种不同的方法在 ProgressBar 控件中显示给定的值。选择的方法将取决于您所要执行的任务或要解决的问题:
- 直接设置 ProgressBar 控件的值。如果您知道任务中涉及的测量项总数,此方法非常有用,例如从数据源中读取记录。此外,如果您只需要设置该值一次或两次,这将是实现该目的的简便方法。最后,如果您需要减小进度栏显示的值,请使用此方法。
- 使 ProgressBar 的显示值按固定值递增。如果需要显示最小值和最大值之间的简单计数,此方法非常有用,例如运行时间或已知文件总数中已处理的文件数。
- 使 ProgressBar 的显示值按可变值递增。如果需要多次以不同数量更改显示值,此方法非常有用。例如,显示将一系列文件写入磁盘时所占用的硬盘空间量。
直接设置 ProgressBar 值
设置进度栏显示的值的最直接的方法是设置 Value 属性。这可以在设计时或运行时完成。
直接设置 ProgressBar 值
- 设置 ProgressBar 控件的 Minimum 和 Maximum 值。
- 在代码中,将该控件的 Value 属性设置为已设定的最小值和最大值之间的整数值。
注意如果为 Value 属性设置的值超出了由 Minimum 和 Maximum 属性确定的边界,则该控件将引发 ArgumentException 异常。
以下示例阐释如何直接设置 ProgressBar 值。该代码从数据源中读取记录,并在每次读取数据记录时更新进度栏和标签。本示例假设您的窗体具有一个 Label 控件、一个 ProgressBar 控件以及一个数据表,该数据表中名为 CustomerRow
的行具有 FirstName
和 Last Name
字段。
' Visual Basic
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
// C#
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 控件的值按该时间间隔递增。对于计时器以及其他一些您无法以整体的百分比测量进度的方案,这是非常有用的。
使进度栏按固定值递增
- 设置 ProgressBar 控件的 Minimum 和 Maximum 值。
- 将控件的 Step 属性设置为一个整数,该整数代表进度栏的显示值增加的数量。
- 调用 PerformStep 方法,使显示值按 Step 属性中设置的数量更改。
以下示例阐释进度栏如何维护复制操作中的文件计数。
在以下示例中,将每个文件读入内存时,进度栏和标签都会进行更新,以反映读取的文件总数。此示例假定您的窗体具有 Label 控件和 ProgressBar 控件。
' Visual Basic
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
// C#
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 值按可变数量递增
最后,您可以使进度栏的显示值每次增加的数量都是唯一的。这在您记录一系列唯一的操作时非常有用,例如将不同大小的文件写入硬盘,或者按整体的百分比测量进度。
使进度栏按动态值递增
- 设置 ProgressBar 控件的 Minimum 和 Maximum 值。
- 调用 Increment 方法,使显示值按指定的整数更改。
以下示例阐释在复制操作期间,进度栏如何计算已使用的磁盘空间量。有关确定当前可用的磁盘空间的更多信息,请参见代码:查找可用的磁盘空间量 (Visual Basic)。
在以下示例中,将每个文件写入硬盘时,进度栏和标签都会进行更新,以反映可用的硬盘空间量。此示例假定您的窗体具有 Label 控件和 ProgressBar 控件。
' Visual Basic
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
// C#
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();
}
}
请参见
Windows 窗体 ProgressBar 控件介绍 | ProgressBar 控件(Windows 窗体) | ProgressBar 类
[C#]
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();
}
}
}
<!-- End Content -->
分享到:
相关推荐
通过ODBC链接Progress文件 查询Progress数据表 操作(增删改)Progress表
一个C# progress bar 自定义控件,类似visual studio 的安装进度显示,可以显示当前进度的具体状态
使用步骤: 1、把ODBCx86文件夹或ODBCx64文件夹放在C盘根目录下。 2.1、运行ODBCx86\scripts文件夹下的‘将配置写入32位目录.reg’和‘将配置写入64位目录.reg’ 2.2、运行ODBCx64\scripts文件夹下的‘将配置写入32...
摘要:C#源码,系统相关,进度条,多线程 C#多线程Progress进度条源码,要点:使用委托和异步调用,刷新进度条,标记为后台进程,在窗口退出时,正常退出。 运行环境:Visual Studio2010
大量C#的自定义进度条,圆滑的,多边形的。有控件,有实例。
好看的progressbar 样式,弥补winform 默认样式的单调。
backgroundworker+process进度条完整解决方案,以测试通过
C#多线程Progress进度条源码,要点:使用委托和异步调用,刷新进度条,标记为后台进程,在窗口退出时,正常退出。
c# progress 异步显示 进度条 不卡死 顺畅 方便 简洁 可应用于大型数据计算、平差、分析、建模和预测
winform 进度条 ProgressBar 带进度显示,带源码
需要安装progress的ODBC驱动,这个可以扩张到其它类型的数据传送,如SQL到ORACLE
最近使用 C# winform 做了一个管理小工具,项目中遇到了读取处理大文件数据,逻辑处理周期较长,所有特意搞了个等待窗口,简单实用,程序可以正常使用,分享下!
基于C#+Silverlight 2实现的a Progress Bar 进度条显示控件源码。
• Weight Monitor – Track your weight each day and monitor your progress toward established goals. • Home Inventory Manager – Helps you keep track of all your belongings – even includes ...
.net bar (Winform)的.效果非常之眩.! ,window xp,vs2005,Office 2007,Office 2010.等多种风格的控件.!
摘要:C#源码,数据库应用,ProgressBar,进度条 C#在ProgressBar控件中显示进度百分比,也就是添加一个进度条的应用,进度条大家应该都知道吧,很多需要用户等待的时候,一般都会使用进度条来显示进度,提升软件体验,...
在“新建项目”对话框的项目类型窗口中选中“Visual C#”作为项目开发语言,在模板窗口中选中“Windows 应用程序”作为项目开发模板,在“名称”、“位置”编辑框中输入自己设定的项目名字和项目存储位置,本讲义...
C#程序作为调用这需要生成一个Progress类,该类提供了调用EXE可执行文件所用到的属性和事件. System.Diagnostics.Process pExecuteEXE = new System.Diagnostics.Process(); pExecuteEXE.StartInfo.FileName = @"E:\...
找了好久,终于找到一款既好用又漂亮的C#进度条,有需要的朋友请拿去;
C# WPF 解压缩7zip文件 带进度条 sevenzipsharp WPF PNG实现的图形进度条 .NET 3.5 vs2013打开 来这里看介绍 http://www.cnblogs.com/xe2011/p/3761870.html