如何使用进度条:
一、添加控件
Panel panelBack=new Panel();
ProgressBar progressBarExcelExport=new ProgressBar();
Label lblCaption=new Label();
二、 初始化设置
panelBack.Visable=false;
progressBarExcelExport.Visable=false;
lblCaption.Visable=false;
三、在需要显示进度条的函数里添加
Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;//获取屏幕高度,宽度
progressBarExcelExport.Minimum = 0;
progressBarExcelExport.Maximum = 100;
panelBack.Top = 0;
panelBack.Left = 0;
panelBack.Width = this.Width;
panelBack.Height = this.Height;
progressBarExcelExport.SetBounds((this.Width - this.Width / 2) / 2, this.Height / 2 - 100, this.Width / 2, 30);
progressBarExcelExport.BackColor = Color.Green;
panelBack.BringToFront();
lblCaption.BringToFront();
progressBarExcelExport.BringToFront();
lblCaption.Left = progressBarExcelExport.Left+15 ;
lblCaption.Top = progressBarExcelExport.Top-17;
progressBar1.Show();
//写入数值
panelBack.Visible = true;
lblCaption.Visible = true;
Application.DoEvents();
string oldCaption = "";
long totalCount = dt.Rows.Count;
long rowRead = 0;
decimal percent = 0;
///在for循环中计算当前进度结果并显示
for (int r = 0; r < dt.Rows.Count; r++)
{
percent = (decimal)(((decimal)100 * rowRead) / totalCount);
this.lblCaption.Text = "正在导出数据[" + percent.ToString("0.00") + "%]...";
progressBarExcelExport.Value = Convert.ToInt32(percent);
Application.DoEvents();
}
///进度走完关闭进度条
this.lblCaption.Visible = false;this.lblCaption.Text = oldCaption;
progressBarExcelExport.Hide();
panelBack.Visible = false;