| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7175 人关注过本帖
标题:progressBar怎么用
只看楼主 加入收藏
Mas_Xu
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-11-14
收藏
 问题点数:0 回复次数:2 
progressBar怎么用
progressBar怎么用,最好给个示例程序
搜索更多相关主题的帖子: progressBar 
2008-11-14 22:08
coolszy
Rank: 1
来 自:编程论坛
等 级:新手上路
帖 子:241
专家分:5
注 册:2007-11-3
收藏
得分:0 

设置进度栏的显示值的最直接方式是设置 Value 属性。该操作可以在设计时或运行时进行。

直接设置 ProgressBar 值
设置 ProgressBar 控件的 Minimum 和 Maximum 值。

在代码中,将该控件的 Value 属性设置为已设定的最小值与最大值之间的一个整数值。

注意
如果将 Value 属性设置为 Minimum 和 Maximum 属性设定的边界之外的值,该控件将引发 ArgumentException 异常。
 

下面的代码示例说明如何直接设置 ProgressBar 值。该代码从数据源中读取记录,并在每次读取数据记录时更新进度栏和标签。该示例要求窗体有一个 Label 控件、一个 ProgressBar 控件以及一个数据表,该数据表中名为 CustomerRow 的行具有 FirstName 和 Last Name 字段。
 
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 控件的 Minimum 和 Maximum 值。

将控件的 Step 属性设置为一个整数,该整数代表进度栏的显示值递增的数量。

调用 PerformStep 方法,使显示值按 Step 属性中设置的数量进行更改。

下面的代码示例说明进度栏如何维护复制操作中的文件计数。

在下面的示例中,当每个文件读入内存时,进度栏和标签都会相应地更新,以反映读取的文件总数。该示例要求窗体有一个 Label 控件和一个 ProgressBar 控件。
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 控件的 Minimum 和 Maximum 值。

调用 Increment 方法,使显示值按指定的整数进行更改。

下面的代码示例说明在复制操作期间,进度栏如何计算已使用的磁盘空间量。

在下面的示例中,当每个文件写入硬盘时,进度栏和标签都会相应地更新,以反映可用的硬盘空间量。该示例要求窗体有一个 Label 控件和一个 ProgressBar 控件。
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();
   }
}



最好去看看MSDN

[[it] 本帖最后由 coolszy 于 2008-11-14 22:24 编辑 [/it]]

天道酬勤
2008-11-14 22:20
guoleopard
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-9-15
收藏
得分:0 
顶一个,不错的贴~~
2008-11-16 19:08
快速回复:progressBar怎么用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014583 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved