一款给客户超Sweet感觉的软件怎么能少了进度条呢?无论你是用于文件传输、自动测试、通讯交互等,都可以用进度条进行实时反馈,其实做软件开发不需要设计太多花俏的功能,只要在这些细节上面稍微做好功夫就可以分分钟提高客户对你这个软件的好感啦~今天我就给大家分享一个超级简单的进度条应用案例,让你3分钟掌握C#的ProgressBar控件,走过路过不要错过!
主界面如图1所示,主要有操作栏和信息栏,操作栏可以设置进度条长度、开始、暂停和结束,信息栏实时显示进度信息。进度条处理的方法:如果有一批文件或者一系列流程,首先获得这批文件或流程的数量count=文件或流程数量,设置进度条的范围为0至count,每完成一个文件或流程就向前进一步,从而实现实时反馈的效果。
首先定义了一个output函数,用于打印进度信息。
private void output(string info) { textBox2.Text = ""; textBox2.AppendText(DateTime.Now.ToString("HH:mm:ss") + info + "\r\n");//打印时间和文本信息 }
其次使用timer让进度值自增,模拟程序实时运行。
private void Timer1_Tick(object sender, EventArgs e) { if (progressBar1.Value < progressBar1.Maximum) { progressBar1.Value++;//进度值自增 output("进度运行中(" + progressBar1.Value.ToString() + "/" + progressBar1.Maximum + ")..."); } else { output("进度完成"); timer1.Enabled = false; } }
然后分别设置”开始”、”暂停”和”结束”按钮的后台代码。
private void Button1_Click(object sender, EventArgs e) { if (textBox1.Text == "") { return; } else { progressBar1.Value = 0; progressBar1.Minimum = 0; progressBar1.Maximum = int.Parse(textBox1.Text); output("进度条开始"); timer1.Enabled = true; } }
private void Button2_Click(object sender, EventArgs e) { if (timer1.Enabled == true) { output("进度条暂停"); button2.Text = "继续"; timer1.Enabled = false; } else { output("进度条继续"); button2.Text = "暂停"; timer1.Enabled = true; } }
private void Button3_Click(object sender, EventArgs e) { output("进度条停止"); progressBar1.Value = 0; }
编译运行,输入进度条长度,点击”开始”按钮就可以看到进度条运行了,超级简单快来试一试吧~
代码全文:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace progressBar { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Button1_Click(object sender, EventArgs e) { if (textBox1.Text == "") { return; } else { progressBar1.Value = 0; progressBar1.Minimum = 0; progressBar1.Maximum = int.Parse(textBox1.Text); output("进度条开始"); timer1.Enabled = true; } } private void Button2_Click(object sender, EventArgs e) { if (timer1.Enabled == true) { output("进度条暂停"); button2.Text = "继续"; timer1.Enabled = false; } else { output("进度条继续"); button2.Text = "暂停"; timer1.Enabled = true; } } private void Button3_Click(object sender, EventArgs e) { output("进度条停止"); progressBar1.Value = 0; } private void output(string info) { textBox2.Text = ""; textBox2.AppendText(DateTime.Now.ToString("HH:mm:ss") + info + "\r\n");//打印时间和文本信息 } private void Timer1_Tick(object sender, EventArgs e) { if (progressBar1.Value < progressBar1.Maximum) { progressBar1.Value++;//进度值自增 output("进度运行中(" + progressBar1.Value.ToString() + "/" + progressBar1.Maximum + ")..."); } else { output("进度完成"); timer1.Enabled = false; } } private void Button4_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("https://www.daboke.com");//欢迎访问大博客,探索更多编程实战案例! } private void Button5_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("https://www.daboke.com/program/progressbar.html");//原文链接! } private void Button6_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("https://space.bilibili.com/580719958");//Bilibili-编程实战视频,Up主:编程自修室! } } }
B站视频教程:https://www.bilibili.com/video/BV1mQ4y1A7Za
源码下载:progressBar Or https://pan.baidu.com/s/119VulhHJJC8xARfANpmskQ 提取码:o39p