Here is a picture of a progress bar i made that shows the percent in the middle.
That wasn't Photoshoped, it's a cropped screenshot from my actual application. That uses size 8.25 Arial font. Believe it or not, that only took 2 lines of code after i changed the progress bar's value.
And... here it is
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));
Let's analyze that. The percent variable is the calculated percent of the the progress bar's value. Now, the next line is a bit more complicated. First, it is using the DrawString() method of the Systm.Drawing.Graphics class to draw text in the progress bar. Let's analyze each argument:
1. percent.ToString() + "%"
That's really self-explanatory. It says what text will be displayed.
2. new Font("Arial", (float)8.25, FontStyle.Regular)
That is the font that is displayed.
That is the color of the text. That can be easily changed. It can be a SolidBrush, which is a brush of a solid color. Or, if you want to get fancy, it can be a TextureBrush. A Texture Brush has a texture defined by an image to assign it.
4. new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7)
That is where the text will be drawn. It defines the upper-left corner. This is the most important part and needs the most explaining. Let's take a look at how i arrived at those equations to center the text:
----- X Position: progressBar1.Width / 2 - 10
The "progressBar1.Width / 2" part will return the center point (in pixels) of the progress bar horizontally. Now, the "- 10" part is used to offset the text so it will be centered horizontally. If it wasn't there, the text would start at the center of the progress bar, and would be offset to the right. I know of no equation that, given the font's em-size and height, can calculate exactly the offset needed to accomplish this, but i find with size-8.25 Arial and 3 chars, 10 works fine.
----- Y Position: progressBar1.Height / 2 - 7
Like with the X point, "progressBar1.Height / 2" calculates the center point of the progress bar vertically. Also like the X point, the "- 7" part is used to offset the text so it will be centered vertically. Again, i know of no equation that will do this, but for size-8.25 Arial, 7 works fine for a vertical offset.
Those exact lines of code above is what i used to draw the picture at the top. All you have to do is, after you change the progress bar's value, put that code right after it. There is no need to refresh the progress bar, because changing the value does this for you. Of course, my progress bar was called "progressBar1", but that can be easily changed. All it takes is a copy/paste. Enjoy!