关于progressbar

 

private void btnStart_Click(object sender, EventArgs e)

        {

          pbrProgress.Maximum = 100;//设置最大长度值           

            this.Cursor = Cursors.WaitCursor;

            ///添加一个事件处理程序   

            backgroundWorker1.DoWork += new DoWorkEventHandler(DoWork);

           ///添加一个显示进度条的事件   

            this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged); //ProgressChanged 是显示进度条Method   

            ///后台已操作完成,关闭窗体   

            this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CloseForm); // CloseForm()调用关闭窗体   

            ///开始执行后台操作   

            this.backgroundWorker1.RunWorkerAsync(filename);

        }

            private void DoWork(object sender , DoWorkEventArgs e)   

            {   

                e.Result = (WorkTimer(this.backgroundWorker1,e));   

            } 

            /// <summary>   

        /// 设置进度条显示长度   

          /// </summary>   

        /// <param name="sender"></param>   

        /// <param name="e"></param>   

       

        private int WorkTimer(object sender, DoWorkEventArgs e)  //此为自己的操作处理函数,切记!! 

        {

            int max = pbrProgress.Maximum;

            int percent = 0;



            for (int i = 0; i <= max; i++)   

            {   

                if (this.backgroundWorker1.CancellationPending)   

                {   

                    e.Cancel = true;   

                    return -1;   

                }   

                else 

                {   

                    //自己的函数处理计算过程

                      ......  





                    percent = (int)(((double)i / (double)max) * 100);

                    this.backgroundWorker1.ReportProgress(percent);   //单次循环处理完报告进度,触发ProgressChanged事件                

                }   

                ///   将当前线程挂起指定的时间   

                System.Threading.Thread.Sleep(100);   

            }   

            return -1;   

        }   

        //显示进度条

        public void ProgressChanged(object sender, ProgressChangedEventArgs e)   

        {   

            this.pbrProgress.Style = ProgressBarStyle.Continuous;   

            this.lblMsg.Text=string.Format("已完成{0}%,请稍候...", e.ProgressPercentage);

            this.pbrProgress.Value = e.ProgressPercentage;   

        }   

         /// <summary>   

        /// 关闭进度条显示窗体   

        /// </summary>   

        /// <param name="sender"></param>   

        /// <param name="e"></param>   

        public void CloseForm(object sender, RunWorkerCompletedEventArgs e)   

        {   

            this.Close();   

        }

  

Reference:http://blog.csdn.net/x415831943/article/details/7795761

http://www.cnblogs.com/xuqiang/archive/2010/03/29/1953690.html

 

另外关于如何在ProgressBar 添加文字

虽然ToolStripProgressBar.Text 屬性和ProgressBar.Text属性均存在,但不可用,“此 API 支持 .NET Framework 基础结构,不适合在代码中直接使用。”具体参考http://msdn.microsoft.com/zh-cn/library/vstudio/6fetw5dd(v=vs.90).aspx

以下为解决方法:

private void pbPrecentage(ToolStripProgressBar pb)

{

    int percent = (int)(((double)(pb.Value - pb.Minimum) /

    (double)(pb.Maximum - pb.Minimum)) * 100);



    using (Graphics gr = pb.ProgressBar.CreateGraphics())

    {

        //Switch to Antialiased drawing for better (smoother) graphic results

        gr.SmoothingMode = SmoothingMode.AntiAlias;

        gr.DrawString(percent.ToString() + "%",

            SystemFonts.DefaultFont,

            Brushes.Black,

            new PointF(pb.Width / 2 - (gr.MeasureString(percent.ToString() + "%",

                SystemFonts.DefaultFont).Width / 2.0F),

            pb.Height / 2 - (gr.MeasureString(percent.ToString() + "%",

                SystemFonts.DefaultFont).Height / 2.0F)));

    }

}

  //Activate Double Buffering for all kind of drawing within your form
this.SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint|ControlStyles.DoubleBuffer,true);

以上用于窗体设置,如不设置类型则progressbar无text
Reference:http://stackoverflow.com/questions/7708904/adding-text-to-the-tool-strip-progress-bar

你可能感兴趣的:(ProgressBar)