
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
2.7 案例分析7 使用Timer控件和进度条
2.7.1 案例描述
本节介绍如何创建并使用Timer控件和ProgressBar控件。
2.7.2 案例分析
Timer控件的Tick事件用法和ProgressBar控件的Value属性。
2.7.3 案例实现与技巧
① 首先,运行Visual Studio 2005,创建一个名为ProgressBar的Windows应用程序。
② 向窗体中拖曳一个ProgressBar控件。
③ 向窗体中拖曳一个Timer控件。
④ 在“属性”窗口中,修改“Timer”控件的属性Interval值为“400”。
⑤ 为“Timer”控件添加事件处理程序timer1_Tick(),如下所示:
private void timer1_Tick(object sender,EventArgs e) { if (progressBar1.Value >= 100) { progressBar1.Value = 0; label1.Text = "进度" + progressBar1.Value.ToString() + "%"; return; } progressBar1.Value += 5; label1.Text = "进度" + progressBar1.Value.ToString() + "%"; }
⑥ 向窗体拖曳一个Button控件,修改其Text属性为“开始”。
⑦ 双击Button控件,添加如下的事件处理程序。
timer1.Enabled = true;
⑧ 再向窗体拖曳一个Button控件,修改其Text属性为“暂停”。
⑨ 双击此Button控件,添加如下的事件处理程序:
timer1.Enabled = false;
⑩ 在窗体设计器中选择Timer控件,修改其Enabled属性为false。
⑪ 向Form1窗体拖曳一个Label控件,修改其Text属性为“进度0%”。
⑫ 运行该程序,结果如图2-17所示。

图2-17 程序运行结果