Windows Forms: Custom color of Progress Bar in C#

This tutorial shows you how to custom background color of Progress Bar in C#.NET Windows Forms Application.

To play the demo, you should create a new windows forms project, then create a CustomProgressBar class.

To custom progress bar background color in c#, you need to make your class inherit from the ProgressBar control, then override the OnPaint method as shown below.

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

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);
        double scaleFactor = (((double)Value - (double)Minimum) / ((double)Maximum - (double)Minimum));
        if (ProgressBarRenderer.IsSupported)
            ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rec);
        rec.Width = (int)((rec.Width * scaleFactor) - 4);
        rec.Height -= 4;
        LinearGradientBrush brush = new LinearGradientBrush(rec, this.ForeColor, this.BackColor, LinearGradientMode.Vertical);
        e.Graphics.FillRectangle(brush, 2, 2, rec.Width, rec.Height);
    }
}

Rebuild your project, then you can see the CustomProgressBar control in your visual toolbox, drag the CustomProgressBar and Button from your visual studio toolbox to your windows forms application. You can layout your winform as shown below.

custom color of progress bar in c#

Finally, Add code behind to handle Form_Load and Start_Click event as shown belown.

private void btnStart_Click(object sender, EventArgs e)
{
    for (int i = 1; i <= 100; i++)
    {
        customProgressBar1.Value = i;
        Thread.Sleep(100);
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    customProgressBar1.ForeColor = Color.FromArgb(120, 0, 0);
    customProgressBar1.BackColor = Color.FromArgb(172, 0, 0);
}

At the Form_Load event you should set the background color for your progress bar.