i have made a cool progressbar with text in the middle of it. Here is my code:
private void timer2_Tick(object sender, EventArgs e)
{
progressBar1.Increment(+1);
int percent = progressBar1.Value;
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)
);
if (progressBar1.Value >= 99)
{
timer2.Stop();
this.Close();
}
}
For some reason, the text shows up then disappears and other weird stuff. Why is that, and how do i fix it?
Try moving the drawing code into the Paint event, you are basically modifying the control’s visuals, so you need to handle this instead of the default painting behaviour.