Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi
My progressbar perecentage disappears when the progressbar updates why is this?

CODE:

C#
public void PBPercentage()
      {

          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)));


          }
      }
Posted

1 solution

Because you graphics is volatile. You should not draw using the instance of System.Drawing.Graphics you create. Instead, you should do it in an overridden method System.Windows.Forms.Control.OnDraw or your handler of the event System.Windows.Forms.Control.Draw and use the Graphics instance passed in the event arguments parameter. Please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900