Search code examples
c#system.drawing

Adding text to the Tool Strip Progress Bar


I am trying to add some text to a Tool Strip Progress Bar but I have thus far been unsucessful, here is some code I found HERE:

private void pbPrecentage(ToolStripProgressBar pb)
{
    ProgressBar p = new ProgressBar();

    int percent = (int)(((double)(pb.Value - pb.Minimum) /
    (double)(pb.Maximum - pb.Minimum)) * 100);
    using (Graphics gr = pb.CreateGraphics())
    {
        gr.DrawString(percent.ToString() + "%",
            SystemFonts.DefaultFont,
            Brushes.Black,
            new PointF(pb.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
                SystemFonts.DefaultFont).Width / 2.0F),
            pb.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
                SystemFonts.DefaultFont).Height / 2.0F)));
    }
}

The problem is that the Tool Strip Progress Bar doesn't has a CreateGraphics method. So I was wondering if anyone was able to successfully add text to a Tool Strip Progress Bar.

UPDATE

Ok, it seems that the ToolStripProgressBar has a progress bar property which in turn has the CreateGraphics method, but the problem now is that the text value is blinking and flashing, how would I go about fixing this? Here is the revised code:

private void pbPrecentage(ToolStripProgressBar pb)
{
    int percent = (int)(((double)(pb.Value - pb.Minimum) /
    (double)(pb.Maximum - pb.Minimum)) * 100);

    using (Graphics gr = pb.ProgressBar.CreateGraphics())
    {
        gr.DrawString(percent.ToString() + "%",
            SystemFonts.DefaultFont,
            Brushes.Black,
            new PointF(pb.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
                SystemFonts.DefaultFont).Width / 2.0F),
            pb.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
                SystemFonts.DefaultFont).Height / 2.0F)));
    }
}

Solution

  • You are running into a very common problem concerning Windows Forms, which is based on GDI+. The standard setting for Double-Buffering for the drawing of controls (and user drawing contexts like graphics) is off. So just add some lines of code to your form:

    public Form1()
    {
    //Activate Double Buffering for all kind of drawing within your form
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
    }
    

    If you wanna additionally get nicer graphics then you also should use a SmoothingMode for your drawing context:

    private void pbPrecentage(ToolStripProgressBar pb)
    {
        int percent = (int)(((double)(pb.Value - pb.Minimum) /
        (double)(pb.Maximum - pb.Minimum)) * 100);
    
        using (Graphics gr = pb.ProgressBar.CreateGraphics())
        {
            //Switch to Antialiased drawing for better (smoother) graphic results
            gr.SmoothingMode = SmoothingMode.AntiAlias;
            gr.DrawString(percent.ToString() + "%",
                SystemFonts.DefaultFont,
                Brushes.Black,
                new PointF(pb.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
                    SystemFonts.DefaultFont).Width / 2.0F),
                pb.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
                    SystemFonts.DefaultFont).Height / 2.0F)));
        }
    }