0

Why does my progressbar percentage text disapear when progressbar starts loading?? Am I missing out anything?

int percent = (int)(((double)(progressBar1.Value - progressBar1.Minimum) /
                     (double)(progressBar1.Maximum - progressBar1.Minimum)) * 100);
using (Graphics gr = progressBar1.CreateGraphics())
{
    gr.DrawString(percent.ToString() + "%",
    SystemFonts.DefaultFont,
    Brushes.Black,
    new PointF(progressBar1.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
    SystemFonts.DefaultFont).Width / 2.0F),
    progressBar1.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
    SystemFonts.DefaultFont).Height / 2.0F)));
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98

1 Answers1

0

To draw on top of a winforms control, you will want to bind to the control's OnPaint event and do your drawing within that event handler.

Controls are repainted at various times, such as when the form is resized or in this case when the progress value is updated. This event fires after the control has been re-drawn.

As others have mentioned, if you try to draw on a winforms control at another time, it will likely just be overwritten when the control is repainted.

The PaintEventArgs object provided by the OnPaint event has a Graphics object you should use to draw (instead of using the CreateGraphics() method as you have).

Jim Noble
  • 492
  • 6
  • 12