Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 10k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Threading;
  8. namespace ch1_10
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.ProgressBar progressBar1;
  16. private System.Windows.Forms.Button button1;
  17. private System.Windows.Forms.GroupBox groupBox1;
  18. private System.Windows.Forms.Label label1;
  19. private System.Windows.Forms.ComboBox comboBox1;
  20. private System.Windows.Forms.Label label2;
  21. private System.Windows.Forms.TrackBar trackBar1;
  22. //代表进程等待时间
  23. private int waitTime=150;
  24. //申明一个进程对象
  25. private Thread barProgress;
  26. private System.Windows.Forms.Label label3;
  27. private System.Windows.Forms.Button button2;
  28. private System.Windows.Forms.Label completedlabel;
  29. private System.Windows.Forms.Button button3;
  30. private System.Windows.Forms.Button button4;
  31. /// <summary>
  32. /// 必需的设计器变量。
  33. /// </summary>
  34. private System.ComponentModel.Container components = null;
  35. public Form1()
  36. {
  37. //
  38. // Windows 窗体设计器支持所必需的
  39. //
  40. InitializeComponent();
  41. //
  42. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  43. //
  44. }
  45. /// <summary>
  46. /// 清理所有正在使用的资源。
  47. /// </summary>
  48. protected override void Dispose( bool disposing )
  49. {
  50. if( disposing )
  51. {
  52. if (components != null) 
  53. {
  54. components.Dispose();
  55. }
  56. }
  57. base.Dispose( disposing );
  58. }
  59. #region Windows Form Designer generated code
  60. /// <summary>
  61. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  62. /// 此方法的内容。
  63. /// </summary>
  64. private void InitializeComponent()
  65. {
  66. this.progressBar1 = new System.Windows.Forms.ProgressBar();
  67. this.button1 = new System.Windows.Forms.Button();
  68. this.groupBox1 = new System.Windows.Forms.GroupBox();
  69. this.trackBar1 = new System.Windows.Forms.TrackBar();
  70. this.label2 = new System.Windows.Forms.Label();
  71. this.comboBox1 = new System.Windows.Forms.ComboBox();
  72. this.label1 = new System.Windows.Forms.Label();
  73. this.label3 = new System.Windows.Forms.Label();
  74. this.completedlabel = new System.Windows.Forms.Label();
  75. this.button2 = new System.Windows.Forms.Button();
  76. this.button3 = new System.Windows.Forms.Button();
  77. this.button4 = new System.Windows.Forms.Button();
  78. this.groupBox1.SuspendLayout();
  79. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
  80. this.SuspendLayout();
  81. // 
  82. // progressBar1
  83. // 
  84. this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
  85. this.progressBar1.Location = new System.Drawing.Point(0, 250);
  86. this.progressBar1.Name = "progressBar1";
  87. this.progressBar1.Size = new System.Drawing.Size(408, 23);
  88. this.progressBar1.Step = 1;
  89. this.progressBar1.TabIndex = 0;
  90. // 
  91. // button1
  92. // 
  93. this.button1.Location = new System.Drawing.Point(16, 160);
  94. this.button1.Name = "button1";
  95. this.button1.TabIndex = 1;
  96. this.button1.Text = "开始";
  97. this.button1.Click += new System.EventHandler(this.button1_Click);
  98. // 
  99. // groupBox1
  100. // 
  101. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  102. this.trackBar1,
  103. this.label2,
  104. this.comboBox1,
  105. this.label1});
  106. this.groupBox1.Location = new System.Drawing.Point(16, 24);
  107. this.groupBox1.Name = "groupBox1";
  108. this.groupBox1.Size = new System.Drawing.Size(368, 120);
  109. this.groupBox1.TabIndex = 2;
  110. this.groupBox1.TabStop = false;
  111. this.groupBox1.Text = "设置";
  112. // 
  113. // trackBar1
  114. // 
  115. this.trackBar1.Location = new System.Drawing.Point(168, 64);
  116. this.trackBar1.Maximum = 150;
  117. this.trackBar1.Minimum = 10;
  118. this.trackBar1.Name = "trackBar1";
  119. this.trackBar1.Size = new System.Drawing.Size(136, 42);
  120. this.trackBar1.TabIndex = 3;
  121. this.trackBar1.TickStyle = System.Windows.Forms.TickStyle.None;
  122. this.trackBar1.Value = 10;
  123. this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
  124. // 
  125. // label2
  126. // 
  127. this.label2.Location = new System.Drawing.Point(16, 64);
  128. this.label2.Name = "label2";
  129. this.label2.TabIndex = 2;
  130. this.label2.Text = "进程启动速度";
  131. this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  132. // 
  133. // comboBox1
  134. // 
  135. this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  136. this.comboBox1.Items.AddRange(new object[] {
  137.    "1",
  138.    "5",
  139.    "10",
  140.    "20"});
  141. this.comboBox1.Location = new System.Drawing.Point(176, 24);
  142. this.comboBox1.Name = "comboBox1";
  143. this.comboBox1.Size = new System.Drawing.Size(121, 20);
  144. this.comboBox1.TabIndex = 1;
  145. this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  146. // 
  147. // label1
  148. // 
  149. this.label1.Location = new System.Drawing.Point(16, 32);
  150. this.label1.Name = "label1";
  151. this.label1.TabIndex = 0;
  152. this.label1.Text = "进度条的步长";
  153. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  154. // 
  155. // label3
  156. // 
  157. this.label3.Location = new System.Drawing.Point(16, 224);
  158. this.label3.Name = "label3";
  159. this.label3.Size = new System.Drawing.Size(120, 23);
  160. this.label3.TabIndex = 3;
  161. this.label3.Text = "进程已完成的百分比";
  162. // 
  163. // completedlabel
  164. // 
  165. this.completedlabel.Location = new System.Drawing.Point(168, 224);
  166. this.completedlabel.Name = "completedlabel";
  167. this.completedlabel.TabIndex = 4;
  168. // 
  169. // button2
  170. // 
  171. this.button2.Enabled = false;
  172. this.button2.Location = new System.Drawing.Point(112, 160);
  173. this.button2.Name = "button2";
  174. this.button2.TabIndex = 5;
  175. this.button2.Text = "暂停";
  176. this.button2.Click += new System.EventHandler(this.button2_Click);
  177. // 
  178. // button3
  179. // 
  180. this.button3.Enabled = false;
  181. this.button3.Location = new System.Drawing.Point(208, 160);
  182. this.button3.Name = "button3";
  183. this.button3.TabIndex = 6;
  184. this.button3.Text = "继续";
  185. this.button3.Click += new System.EventHandler(this.button3_Click);
  186. // 
  187. // button4
  188. // 
  189. this.button4.Enabled = false;
  190. this.button4.Location = new System.Drawing.Point(304, 160);
  191. this.button4.Name = "button4";
  192. this.button4.TabIndex = 7;
  193. this.button4.Text = "中止";
  194. this.button4.Click += new System.EventHandler(this.button4_Click);
  195. // 
  196. // Form1
  197. // 
  198. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  199. this.ClientSize = new System.Drawing.Size(408, 273);
  200. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  201.   this.button4,
  202.   this.button3,
  203.   this.button2,
  204.   this.completedlabel,
  205.   this.label3,
  206.   this.groupBox1,
  207.   this.button1,
  208.   this.progressBar1});
  209. this.Name = "Form1";
  210. this.Text = "进度条示例";
  211. this.groupBox1.ResumeLayout(false);
  212. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
  213. this.ResumeLayout(false);
  214. }
  215. #endregion
  216. /// <summary>
  217. /// 应用程序的主入口点。
  218. /// </summary>
  219. [STAThread]
  220. static void Main() 
  221. {
  222. Application.Run(new Form1());
  223. }
  224. private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  225. {
  226. //将ComboBox1中选择的值转化为进度栏的步长
  227. progressBar1.Step = Int32.Parse((string)comboBox1.SelectedItem);
  228. }
  229. private void trackBar1_Scroll(object sender, System.EventArgs e)
  230. {
  231. //trackBar拖动时改变等待时间
  232. TrackBar tb = (TrackBar) sender ;
  233. int time = 160 - tb.Value ;
  234. this.waitTime = time ;
  235. }
  236. private void button1_Click(object sender, System.EventArgs e)
  237. {
  238. //实例化一个线程,并引用此线程开始执行时要调用的方法。
  239. //这个方法在线程调用Start()方法时候启动
  240. barProgress = new Thread(new ThreadStart(ProgressProc));
  241. //指示这个线程为后台线程
  242. barProgress.IsBackground = true;
  243. //启动线程
  244. barProgress.Start();
  245. //设置其他按钮的Enabled属性
  246. button2.Enabled=true;
  247. button1.Enabled=false;
  248. button4.Enabled=true;
  249. }
  250. private void ProgressProc() 
  251. {
  252.     //实例化一个MethodInvoker 委托 
  253. MethodInvoker mi = new MethodInvoker(UpdateProgress);
  254. while (true) 
  255. {
  256. Invoke(mi);
  257. int sleepTime = this.waitTime;
  258. Thread.Sleep(sleepTime) ;
  259. }
  260. }
  261. private void UpdateProgress() 
  262. {
  263.     double  completed ;
  264. double  number;
  265. //如果值达到最大,进行重置重新开始
  266. if (progressBar1.Value == progressBar1.Maximum) 
  267. {
  268. progressBar1.Value = progressBar1.Minimum ;
  269. }
  270. else 
  271. {
  272. progressBar1.PerformStep();
  273. }
  274. number=this.progressBar1.Value;
  275. completed   = (number/100) * 100.0 ;
  276. //改变completedLabel的Text属性,代表进程进度
  277. completedlabel.Text = Math.Round(completed).ToString() + "%" ;
  278. }
  279. private void button2_Click(object sender, System.EventArgs e)
  280. {
  281. //线程挂起
  282. barProgress.Suspend();
  283. button3.Enabled=true;
  284.             button4.Enabled=false;
  285. }
  286. private void button3_Click(object sender, System.EventArgs e)
  287. {
  288. //继续线程
  289. barProgress.Resume();
  290. button3.Enabled=false;
  291.             button4.Enabled=true;
  292. }
  293. private void button4_Click(object sender, System.EventArgs e)
  294. {
  295. //结束进程
  296. barProgress.Abort();
  297. //将控件的值全部初始化
  298. progressBar1.Value=0;
  299.             progressBar1.Step=1;
  300.             completedlabel.Text="";
  301. trackBar1.Value=10;
  302. comboBox1.SelectedIndex=0;
  303. //将等待事件初始化
  304. this.waitTime=150;
  305. //按钮功能初始化
  306. button1.Enabled=true;
  307. button2.Enabled=false;
  308. button3.Enabled=false;
  309. button4.Enabled=false;
  310. }
  311. }
  312. }