Reputation:
I am using the following code to display % value in my progress bar.However there is some problem with the display.There is some sort of background color(of the same color as text) just behind the text(i.e. percentage value).Please help
Dim percent As Integer = CInt(Math.Truncate((CDbl(prgProgressBar.Value - prgProgressBar.Minimum) / CDbl(prgProgressBar.Maximum - prgProgressBar.Minimum)) * 100))
Using gr As Graphics = prgProgressBar.CreateGraphics()
gr.DrawString(percent.ToString() & "%", SystemFonts.DefaultFont, Brushes.Green, New PointF(prgProgressBar.Width / 2 - (gr.MeasureString(percent.ToString() & "%", SystemFonts.DefaultFont).Width / 2.0F), prgProgressBar.Height / 2 - (gr.MeasureString(percent.ToString() & "%", SystemFonts.DefaultFont).Height / 2.0F)))
End Using
Upvotes: 0
Views: 2105
Reputation: 12993
Because you draw a string on the progressbar without asking it to repaint first, the old string is still there, that is why the text has the 'background color'.
By calling Invalidate
, you force a repaint on the progress bar, and the old string is erased.
Now you have a new paper to draw new stuff.
Upvotes: 1