博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# progressbar 用法
阅读量:7043 次
发布时间:2019-06-28

本文共 8861 字,大约阅读时间需要 29 分钟。

最近由于工作的需要,开始接触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         ///   6         /// 必需的设计器变量。  7         ///   8         private System.ComponentModel.IContainer components = null;  9  10         ///  11         /// 清理所有正在使用的资源。 12         ///  13         /// 如果应释放托管资源,为 true;否则为 false。 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         ///  26         /// 设计器支持所需的方法 - 不要 27         /// 使用代码编辑器修改此方法的内容。 28         ///  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         #endregion102 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_0111 {12     public partial class Form1 : Form13     {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         //同步更新UI30         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             else45             {46                 this.progressBar1.Maximum = (int)total;47                 this.progressBar1.Value = (int)current;48             }49         }50         public class Downloader51         {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                 try30                 {31 32              // 这两句是连接某个网页的。33                     WebRequest myRequest = WebRequest.Create(url);34                     WebResponse myResponse = myRequest.GetResponse();35                     myResponse.Close();36                 }37                 catch38                 {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#的牛人很多,但是我会把我的错误进行分享,以此为戒

转载于:https://www.cnblogs.com/wq920/p/3309237.html

你可能感兴趣的文章
UVa 514 Rails(经典栈)
查看>>
如何使用 RMAN 异构恢复一些表空间
查看>>
java中如何将byte[]里面的数据转换成16进制字符串
查看>>
Axis2 -POJO
查看>>
Java Date API demo
查看>>
[leetcode] Combination Sum and Combination SumII
查看>>
(二)WebRTC手记之框架与接口
查看>>
hbase集群 常用维护命令
查看>>
deepinmind(转)
查看>>
滴滴顺风车设计总结(原创文章)
查看>>
android 10 事件
查看>>
练习10.9-2
查看>>
变化的区间树状数组,单点查询
查看>>
lastIndexOf方法——获取字符最后的索引
查看>>
一种文件捆绑型病毒研究
查看>>
二进制中1的个数
查看>>
opencv查找轮廓---cvFindContours && cvDrawCountours 用法及例子
查看>>
C# 之 服务端获取远程资源
查看>>
《大话操作系统——扎实project实践派》(8.2)(除了指令集.完)
查看>>
SAP 物料移动类型查询表
查看>>