加载窗体显示一个进度条的实现

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Text; 
  4. using System.Windows.Forms; 
  5. using System.Threading; 
  6. namespace WindowsApplication1 
  7. public class Form1 : Form 
  8. private System.ComponentModel.BackgroundWorker backgroundWorker1; 
  9. private StatusStrip statusStrip1; 
  10. private ToolStripProgressBar toolStripProgressBar1; 
  11. private ToolStripStatusLabel toolStripStatusLabel1; 
  12. private Button button2; 
  13. private Button button1; 
  14. public Form1() : base() { InitializeComponent(); } 
  15. private void InitializeComponent() 
  16. this.button1 = new System.Windows.Forms.Button(); 
  17. this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); 
  18. this.statusStrip1 = new System.Windows.Forms.StatusStrip(); 
  19. this.toolStripProgressBar1 = new System.Windows.Forms.ToolStripProgressBar(); 
  20. this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); 
  21. this.button2 = new System.Windows.Forms.Button(); 
  22. this.statusStrip1.SuspendLayout(); 
  23. this.SuspendLayout(); 
  24. // 
  25. // button1 
  26. // 
  27. this.button1.Location = new System.Drawing.Point(12, 12); 
  28. this.button1.Name = "button1 "
  29. this.button1.Size = new System.Drawing.Size(75, 23); 
  30. this.button1.TabIndex = 0; 
  31. this.button1.Text = "Load "
  32. this.button1.UseVisualStyleBackColor = true
  33. this.button1.Click += new System.EventHandler(this.button1_Click); 
  34. // 
  35. // backgroundWorker1 
  36. // 
  37. this.backgroundWorker1.WorkerReportsProgress = true
  38. this.backgroundWorker1.WorkerSupportsCancellation = true
  39. this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork); 
  40. this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted); 
  41. this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged); 
  42. // 
  43. // statusStrip1 
  44. // 
  45. this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
  46. this.toolStripProgressBar1, 
  47. this.toolStripStatusLabel1}); 
  48. this.statusStrip1.Location = new System.Drawing.Point(0, 244); 
  49. this.statusStrip1.Name = "statusStrip1 "
  50. this.statusStrip1.Size = new System.Drawing.Size(292, 22); 
  51. this.statusStrip1.TabIndex = 1; 
  52. this.statusStrip1.Text = "statusStrip1 "
  53. // 
  54. // toolStripProgressBar1 
  55. // 
  56. this.toolStripProgressBar1.Name = "toolStripProgressBar1 "
  57. this.toolStripProgressBar1.Size = new System.Drawing.Size(100, 16); 
  58. // 
  59. // toolStripStatusLabel1 
  60. // 
  61. this.toolStripStatusLabel1.Name = "toolStripStatusLabel1 "
  62. this.toolStripStatusLabel1.Size = new System.Drawing.Size(175, 17); 
  63. this.toolStripStatusLabel1.Spring = true
  64. // 
  65. // button2 
  66. // 
  67. this.button2.Location = new System.Drawing.Point(134, 12); 
  68. this.button2.Name = "button2 "
  69. this.button2.Size = new System.Drawing.Size(75, 23); 
  70. this.button2.TabIndex = 2; 
  71. this.button2.Text = "Cancel "
  72. this.button2.UseVisualStyleBackColor = true
  73. this.button2.Click += new System.EventHandler(this.button2_Click); 
  74. // 
  75. // Form1 
  76. // 
  77. this.ClientSize = new System.Drawing.Size(292, 266); 
  78. this.Controls.Add(this.button2); 
  79. this.Controls.Add(this.statusStrip1); 
  80. this.Controls.Add(this.button1); 
  81. this.Name = "Form1 "
  82. this.statusStrip1.ResumeLayout(false); 
  83. this.statusStrip1.PerformLayout(); 
  84. this.ResumeLayout(false); 
  85. this.PerformLayout(); 
  86. private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) 
  87. toolStripStatusLabel1.Text = "Please wait ............ "
  88. for (int index = 1; index < 100; index++) 
  89. Thread.Sleep(100); 
  90. if (backgroundWorker1.CancellationPending) 
  91. e.Cancel = true
  92. break
  93. backgroundWorker1.ReportProgress(index); 
  94. private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) 
  95. try 
  96. toolStripProgressBar1.Value = e.ProgressPercentage; 
  97. catch (Exception ) { } 
  98. private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) 
  99. toolStripProgressBar1.Value = 0; 
  100. toolStripStatusLabel1.Text = string.Empty; 
  101. private void button1_Click(object sender, EventArgs e) 
  102. backgroundWorker1.RunWorkerAsync(); 
  103. private void button2_Click(object sender, EventArgs e) 
  104. if (backgroundWorker1.IsBusy) //check backgroundWorker state 
  105. backgroundWorker1.CancelAsync(); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值