This is the code I’m using:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
progressBar1.Refresh();
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)));
}
listBox1.Items.Add( "Converting File: " + e.UserState.ToString());
textBox1.Text = e.ProgressPercentage.ToString();
}
While it’s processing and moving to the right the percentages are some blinkings it’s not smooth enough.
And also when its finishing the process in the end the percentages are gone only the green color is left.
I just tried your code and I see your problem, this is a good question. I researched around and found you need to handle the progress bar’s Paint event and draw on e.Graphics.
Otherwise, your drawing will be overwritten next time the control paints itself.
In general, you should never draw on CreateGraphics(). The best way is to subclass the progressbar.
What you need to do first is create a new class like this
Then goto the InitializeComponent method like below below and update
this.progressBar1 = System.Windows.Forms.ProgressBar();to
this.progressBar1 = new MyProgressBar();This should now work as you wanted. You code will then be reduced to this