- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace WindowsApplication1
- {
- public class Form1 : Form
- {
- private System.ComponentModel.BackgroundWorker backgroundWorker1;
- private StatusStrip statusStrip1;
- private ToolStripProgressBar toolStripProgressBar1;
- private ToolStripStatusLabel toolStripStatusLabel1;
- private Button button2;
- private Button button1;
- public Form1() : base() { InitializeComponent(); }
- private void InitializeComponent()
- {
- this.button1 = new System.Windows.Forms.Button();
- this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
- this.statusStrip1 = new System.Windows.Forms.StatusStrip();
- this.toolStripProgressBar1 = new System.Windows.Forms.ToolStripProgressBar();
- this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
- this.button2 = new System.Windows.Forms.Button();
- this.statusStrip1.SuspendLayout();
- this.SuspendLayout();
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(12, 12);
- this.button1.Name = "button1 ";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 0;
- this.button1.Text = "Load ";
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // backgroundWorker1
- //
- this.backgroundWorker1.WorkerReportsProgress = true;
- this.backgroundWorker1.WorkerSupportsCancellation = true;
- this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
- this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
- this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
- //
- // statusStrip1
- //
- this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
- this.toolStripProgressBar1,
- this.toolStripStatusLabel1});
- this.statusStrip1.Location = new System.Drawing.Point(0, 244);
- this.statusStrip1.Name = "statusStrip1 ";
- this.statusStrip1.Size = new System.Drawing.Size(292, 22);
- this.statusStrip1.TabIndex = 1;
- this.statusStrip1.Text = "statusStrip1 ";
- //
- // toolStripProgressBar1
- //
- this.toolStripProgressBar1.Name = "toolStripProgressBar1 ";
- this.toolStripProgressBar1.Size = new System.Drawing.Size(100, 16);
- //
- // toolStripStatusLabel1
- //
- this.toolStripStatusLabel1.Name = "toolStripStatusLabel1 ";
- this.toolStripStatusLabel1.Size = new System.Drawing.Size(175, 17);
- this.toolStripStatusLabel1.Spring = true;
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(134, 12);
- this.button2.Name = "button2 ";
- this.button2.Size = new System.Drawing.Size(75, 23);
- this.button2.TabIndex = 2;
- this.button2.Text = "Cancel ";
- this.button2.UseVisualStyleBackColor = true;
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // Form1
- //
- this.ClientSize = new System.Drawing.Size(292, 266);
- this.Controls.Add(this.button2);
- this.Controls.Add(this.statusStrip1);
- this.Controls.Add(this.button1);
- this.Name = "Form1 ";
- this.statusStrip1.ResumeLayout(false);
- this.statusStrip1.PerformLayout();
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
- {
- toolStripStatusLabel1.Text = "Please wait ............ ";
- for (int index = 1; index < 100; index++)
- {
- Thread.Sleep(100);
- if (backgroundWorker1.CancellationPending)
- {
- e.Cancel = true;
- break;
- }
- backgroundWorker1.ReportProgress(index);
- }
- }
- private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
- {
- try
- {
- toolStripProgressBar1.Value = e.ProgressPercentage;
- }
- catch (Exception ) { }
- }
- private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
- {
- toolStripProgressBar1.Value = 0;
- toolStripStatusLabel1.Text = string.Empty;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- backgroundWorker1.RunWorkerAsync();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if (backgroundWorker1.IsBusy) //check backgroundWorker state
- {
- backgroundWorker1.CancelAsync();
- }
- }
- }
- }
加载窗体显示一个进度条的实现
最新推荐文章于 2026-05-18 07:35:38 发布

1万+

被折叠的 条评论
为什么被折叠?



