C# progressbar 用法

最近由于工作的需要,开始接触c#,发现c#语言真的很强大,本人java出身,学起来没什么难度,但是做网络爬虫的时候遇到一个问题,就是c#的progressbar (进度条)

下面是我参考的内容,希望能有帮助:

原文摘自:http://www.cnblogs.com/kingwangzhen/archive/2010/01/23/1654925.html

http://www.cnblogs.com/tlnature/archive/2009/02/27/1399298.html

 

界面:

  1 namespace ProgressBar_01
  2 {
  3     partial class Form1
  4     {
  5         /// <summary>
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary>
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
 14         protected override void Dispose(bool disposing)
 15         {
 16             if (disposing && (components != null))
 17             {
 18                 components.Dispose();
 19             }
 20             base.Dispose(disposing);
 21         }
 22 
 23         #region Windows 窗体设计器生成的代码
 24 
 25         /// <summary>
 26         /// 设计器支持所需的方法 - 不要
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.progressBar1 = new System.Windows.Forms.ProgressBar();
 32             this.label1 = new System.Windows.Forms.Label();
 33             this.button1 = new System.Windows.Forms.Button();
 34             this.label2 = new System.Windows.Forms.Label();
 35             this.label3 = new System.Windows.Forms.Label();
 36             this.SuspendLayout();
 37             // 
 38             // progressBar1
 39             // 
 40             this.progressBar1.Location = new System.Drawing.Point(157, 142);
 41             this.progressBar1.Name = "progressBar1";
 42             this.progressBar1.Size = new System.Drawing.Size(365, 35);
 43             this.progressBar1.TabIndex = 0;
 44             this.progressBar1.Click += new System.EventHandler(this.progressBar1_Click);
 45             // 
 46             // label1
 47             // 
 48             this.label1.AutoSize = true;
 49             this.label1.Location = new System.Drawing.Point(98, 153);
 50             this.label1.Name = "label1";
 51             this.label1.Size = new System.Drawing.Size(41, 12);
 52             this.label1.TabIndex = 1;
 53             this.label1.Text = "进度:";
 54             this.label1.Click += new System.EventHandler(this.label1_Click);
 55             // 
 56             // button1
 57             // 
 58             this.button1.Location = new System.Drawing.Point(157, 217);
 59             this.button1.Name = "button1";
 60             this.button1.Size = new System.Drawing.Size(75, 23);
 61             this.button1.TabIndex = 2;
 62             this.button1.Text = "提交";
 63             this.button1.UseVisualStyleBackColor = true;
 64             this.button1.Click += new System.EventHandler(this.button1_Click);
 65             // 
 66             // label2
 67             // 
 68             this.label2.AutoSize = true;
 69             this.label2.Location = new System.Drawing.Point(480, 124);
 70             this.label2.Name = "label2";
 71             this.label2.Size = new System.Drawing.Size(0, 12);
 72             this.label2.TabIndex = 3;
 73             // 
 74             // label3
 75             // 
 76             this.label3.AutoSize = true;
 77             this.label3.Location = new System.Drawing.Point(397, 124);
 78             this.label3.Name = "label3";
 79             this.label3.Size = new System.Drawing.Size(77, 12);
 80             this.label3.TabIndex = 4;
 81             this.label3.Text = "进度百分比:";
 82             this.label3.Click += new System.EventHandler(this.label3_Click);
 83             // 
 84             // Form1
 85             // 
 86             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
 87             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
 88             this.ClientSize = new System.Drawing.Size(604, 368);
 89             this.Controls.Add(this.label3);
 90             this.Controls.Add(this.label2);
 91             this.Controls.Add(this.button1);
 92             this.Controls.Add(this.label1);
 93             this.Controls.Add(this.progressBar1);
 94             this.Name = "Form1";
 95             this.Text = "Form1";
 96             this.ResumeLayout(false);
 97             this.PerformLayout();
 98 
 99         }
100 
101         #endregion
102 
103         private System.Windows.Forms.ProgressBar progressBar1;
104         private System.Windows.Forms.Label label1;
105         private System.Windows.Forms.Button button1;
106         private System.Windows.Forms.Label label2;
107         private System.Windows.Forms.Label label3;
108     }
109 }

进度条处理:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace ProgressBar_01
11 {
12     public partial class Form1 : Form
13     {
14         public Form1()
15         {
16             InitializeComponent();
17         }
18 
19         private void progressBar1_Click(object sender, EventArgs e)
20         {
21            
22         }
23         public void StartDownload()
24         {
25             Downloader downloader = new Downloader();
26             downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
27             downloader.Start();
28         }
29         //同步更新UI
30         void downloader_onDownLoadProgress(long total, long current)
31         {
32             if (this.InvokeRequired)
33             {
34                 this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] { total, current });
35                 long test = current + 1;
36                 label2.Text = test + "%";
37                 label2.Show();
38                 if (test == 100)
39                 {
40                     this.Close();
41                     MessageBox.Show("本次任务已完成\n   谢谢使用");
42                 }
43             }
44             else
45             {
46                 this.progressBar1.Maximum = (int)total;
47                 this.progressBar1.Value = (int)current;
48             }
49         }
50         public class Downloader
51         {
52             //委托
53             public delegate void dDownloadProgress(long total, long current);
54             //事件
55             public event dDownloadProgress onDownLoadProgress;
56             //开始模拟工作
57             public void Start()
58             {
59                 for (int i = 0; i < 100; i++)
60                 {
61                     if (onDownLoadProgress != null)
62                         onDownLoadProgress(100, i);
63                     System.Threading.Thread.Sleep(100);
64                 }
65             }
66         }
67 
68         private void button1_Click(object sender, EventArgs e)
69         {
70             //用子线程工作
71             new System.Threading.Thread(new System.Threading.ThreadStart(StartDownload)).Start();
72         }
73     }
74 }


 运行实际效果:

 

 

 1    private void btnRun_Click(object sender, EventArgs e)
 2         {
 3             btnRun.Enabled = false;
 4 
 5             if (txtBoxTarget.Text.Equals(String.Empty) || txtBoxTimes.Text.Equals(String.Empty))
 6             {
 7                 MessageBox.Show("请输入连接的URL和连接次数!", "提示",
 8                     MessageBoxButtons.OK, MessageBoxIcon.Information);
 9                 return;
10             }
11 
12             int length = Int32.Parse(txtBoxTimes.Text.Trim());
13             string url = txtBoxTarget.Text.Trim();
14             double process = 0;
15             int show = 0;
16 
17      DateTime rightNow = DateTime.Now;
18             DateTime end;
19             TimeSpan interval;
20 
21 
22             toolStripStatusLabel.Text = "连接中";
23             progressBar.Visible = true;
24             progressBar.Minimum = 0;
25             progressBar.Maximum = length;
26 
27      for (int i = 1; i <= length; i++)
28             {
29                 try
30                 {
31 
32              // 这两句是连接某个网页的。
33                     WebRequest myRequest = WebRequest.Create(url);
34                     WebResponse myResponse = myRequest.GetResponse();
35                     myResponse.Close();
36                 }
37                 catch
38                 {
39                     txtBoxReport.Text = "网络连接有误!";
40                     return;
41                 }
42 
43                 progressBar.PerformStep();
44 
45                 process = i / length;
46                 show = (int)process * 100;
47             }
48 
49             progressBar.Visible = false;
50             toolStripStatusLabel.Text = "已就绪";
51             txtBoxReport.Text = "连接 " + url + " " + length + "次。";
52 
53             end = DateTime.Now;
54             interval = end - rightNow;
55             txtBoxReport.Text += "\r\n共耗时" + interval.TotalMilliseconds + "毫秒。";
56 
57             btnRun.Enabled = true;
58         }
View Code

 

没有过多的讲解,因为这个我研究的不深,因为c#的牛人很多,但是我会把我的错误进行分享,以此为戒

posted @ 2013-09-09 09:55  wq920  阅读(4660)  评论(0编辑  收藏  举报