2

OK, here is my code for what to paint on the progressbar:

private void timer2_Tick(object sender, EventArgs e)
    {
        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));
        progressBar1.Increment(+1);
        if (progressBar1.Value >= 99)
        {
            timer2.Stop();
            this.Close();
        }

Ok, so i am painting a label in the middle of it that will display the progressbar's value. For some reason, it keeps blinking....disappearing and reappearing. So, someone told me to take out that code and put it in the paint method.....i do not see it. Is there an easier way?

7
  • 1
    Check if setting DoubleBuffered to true makes any difference.
    – SimpleVar
    May 17, 2012 at 20:42
  • where is DoubleBuffered? Is it in the progressbar...or what? I am new, so...sorry May 17, 2012 at 20:44
  • @EliteGamer It is a property of the form itself.
    – SimpleVar
    May 17, 2012 at 20:45
  • Also, why is my form closing so early...it gets to about 97 then stops May 17, 2012 at 20:47
  • I think it is more logical to draw something in the paint method, than doing it from somewhere else. Performance wise I don't know if there's any different. DoubleBuffered can be of great help.
    – RvdK
    May 17, 2012 at 20:50

2 Answers 2

7

Here's a code that should work (I went with option number 3, creating a child Class and overriding the WndProc to handle the paint message:

public class Prog : ProgressBar
{
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        if (m.Msg == 0x000F)
        {
            var flags = TextFormatFlags.VerticalCenter |
                        TextFormatFlags.HorizontalCenter |
                        TextFormatFlags.SingleLine |
                        TextFormatFlags.WordEllipsis;

            TextRenderer.DrawText(CreateGraphics(),
                                  ((float)this.Value/this.Maximum*100) + "%",
                                  Font,
                                  new Rectangle(0, 0, this.Width, this.Height),
                                  Color.Black,
                                  flags);
        }
    }
}
6
  • Elite Gamer: Create an event handler for the control's Paint event. In the event handler body, use the Graphics object from PaintEventArgs parameter to do your drawing. Note that the Paint event may be raised many times, even between two ticks of your timer.
    – Libor
    May 17, 2012 at 20:57
  • I do not see the paint thing for the progressbar? May 17, 2012 at 20:58
  • After verification it seems option 2 is not possible for the particular case of a progress bar. The Paint event is hidden. I would go with option 1.
    – Samy Arous
    May 17, 2012 at 21:00
  • check this for option 3: stackoverflow.com/questions/1517179/…
    – Samy Arous
    May 17, 2012 at 21:01
  • 1
    @lcfseth Edited to make code more readable and elegant (with no annoying scroll bar) and fixed typo "pain message" to "paint message"
    – SimpleVar
    May 17, 2012 at 22:00
1

You can also use ProgressBarRenderer to do the whole drawing by yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.