干货满满!c#进度条ProgressBar学习笔记分享
对于进度条,无碍乎是最小最大值,以及当前值等一些重要的属性,至于方法,用得较多的是PerformStep()和PerformClick()之类的。
代码如下,没有注释,大家凑和看吧。
private void btnRun_Click(object sender, EventArgs e)
{
btnRun.Enabled = false;
if (txtBoxTarget.Text.Equals(String.Empty) || txtBoxTimes.Text.Equals(String.Empty))
{
MessageBox.Show("请输入连接的URL和连接次数!", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
int length = Int32.Parse(txtBoxTimes.Text.Trim());
string url = txtBoxTarget.Text.Trim();
double process = 0;
int show = 0;
DateTime rightNow = DateTime.Now;
DateTime end;
TimeSpan interval;
toolStripStatusLabel.Text = "连接中";
progressBar.Visible = true;
progressBar.Minimum = 0;
progressBar.Maximum = length;
for (int i = 1; i <= length; i++)
{//www.jbxue.com
try
{
// 这两句是连接某个网页的。
WebRequest myRequest = WebRequest.Create(url);
WebResponse myResponse = myRequest.GetResponse();
myResponse.Close();
}
catch
{
txtBoxReport.Text = "网络连接有误!";
return;
}
progressBar.PerformStep();
process = i / length;
show = (int)process * 100;
}
progressBar.Visible = false;
toolStripStatusLabel.Text = "已就绪";
txtBoxReport.Text = "连接 " + url + " " + length + "次。";
end = DateTime.Now;
interval = end - rightNow;
txtBoxReport.Text += "\r\n共耗时" + interval.TotalMilliseconds + "毫秒。";
btnRun.Enabled = true;
}
这是个按钮的实现方法,一旦按下,就会执行代码段中的内容。程序自动连接到某个网页,然后关闭,以此来作为计数,测试进度条是否正常。
运行情况如下:
进度条会慢慢走到终点,程序设计方法正常。
希望以上代码,对大家学习c#进度条(ProgressBar)有所帮助。
相关新闻