2

I'm trying to put the percentage value into my progress bar but it appears like a blink and then disappears.

This is my ProgressChanged event:

C# Code

public void button1_click(object sender, EventArgs e)
{
BackgroundWorker _worker = new BackgroundWorker();
                _worker.WorkerReportsProgress = true;
                _worker.ProgressChanged += new ProgressChangedEventHandler(_worker_ProgressChanged);
                _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);                
                _worker.DoWork += (s, e2) =>
                {
                    for (int i = 0; i <= xmlnode.Count - 1; i++)
                    {
                         _worker.ReportProgress((int)100 * i / (xmlnode.Count - 1));
                         // Many validations here
                    }
                 }
}

void _worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {            
            progressBar1.Value = e.ProgressPercentage;            

             // Here I tryied to put the percentage value in progress bar

            int percent = (int)(((double)progressBar1.Value / (double)progressBar1.Maximum) * 100);
            progressBar1.CreateGraphics().DrawString(percent.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
        }
makc
  • 2,569
  • 1
  • 18
  • 28
Lucas_Santos
  • 4,638
  • 17
  • 71
  • 118
  • 2
    How do you call ReportProgress for your worker? – JleruOHeP Jul 08 '13 at 11:33
  • What blinks and disappears? The ProgressBar control? The bar in the control? The text? – Sebastian Negraszus Jul 08 '13 at 11:36
  • @SebastianNegraszus The text value informing the porcentage in my progress bar. – Lucas_Santos Jul 08 '13 at 11:37
  • @JleruOHeP I updated my question with this information. – Lucas_Santos Jul 08 '13 at 11:46
  • Whatever you draw with CreateGraphics() will be quickly erased again when the ProgressBar repaints itself. Yes, looks like it "blinks". You'll need a different kind of progress bar control, not the one in the toolbox. They are easy to create yourself by deriving from the Control class. – Hans Passant Jul 08 '13 at 11:56
  • possible duplicate of [How to drawn my own progressbar on winforms?](http://stackoverflow.com/questions/3824876/how-to-drawn-my-own-progressbar-on-winforms) – Hans Passant Jul 08 '13 at 11:57
  • The problem is in how you draw text on the progress bar. There are many related questions how to do that, here is one of them: http://stackoverflow.com/questions/10643158/progressbar-paint-method – Bojan Komazec Jul 08 '13 at 11:59

2 Answers2

0

The text is just blinking because it's not redrawn on Paint. Consume the Paint event of the progress bar and draw the percentage there. You can easily grab that percentage by just storing it in a class variable.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

The progress bar is an animated control. When you change its percentage it invalidates its draw surface and completely redraws it. This means that your percentage is drawn for one frame, then obliterated as the animation rolls on.

To fix this, you have to make a new control inheriting from Control or Progress bar and add your code to the OnPaint event that handles your percentage writing. By being in OnPaint, it gets executed every time the control repaints itself, not just the once. Alternatively just hook a delegate onto the existing Paint event of the existing progress bar, which tbh is probably easier.

That would look something like:

//Somewhere during initialisation, this is called once only
progressBar1.Paint+= new PaintEventHandler((object sender, PaintEventArgs e) => 
e.Graphics.DrawString(progressBar1.Value.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7)));
Pharap
  • 3,826
  • 5
  • 37
  • 51