Ivan Prodanov Ivan Prodanov - 17 days ago 5
C# Question

How to change the color of progressbar in C# .NET 3.5?

I'd like to do two things on my progress bar.


  1. Change the green colour to red.

  2. Remove the blocks and make it in one color.



Any information about those two things I wonder how to accomplish will be greatfuly appreaciated!

Thanks.

Answer

Since the previous answers don't appear to work in with Visual Styles. You'll probably need to create your own class or extend the progress bar:

public class NewProgressBar : ProgressBar
{
    public NewProgressBar()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle rec = e.ClipRectangle;

        rec.Width = (int)(rec.Width * ((double)Value / Maximum)) - 4;
        if(ProgressBarRenderer.IsSupported)
           ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle);
        rec.Height = rec.Height - 4;
        e.Graphics.FillRectangle(Brushes.Red, 2, 2, rec.Width, rec.Height);
    }
}

EDIT: Updated code to make the progress bar use the visual style for the background

Comments