How to Change color of Progress Bar in C#

By FoxLearn 11/23/2024 3:27:20 AM   19.54K
In C#, you can change the color of a progress bar by customizing its appearance using the ProgressBar control provided by WinForms or WPF.

Progress bars are a staple of modern user interfaces, helping users visually understand the progress of tasks. In C#, progress bars are commonly implemented in WinForms and WPF applications.

By default, the color of a progress bar is predefined, but customizing its appearance can improve the user experience. This guide explains how to change the color of a WinForm progress bar and a WPF progress bar.

How to change the color of winform progress bar in C#?

In WinForms, the ProgressBar control does not have a built-in property to change its color directly, so you can not be modified unless the visual styles are disabled.

However, you can create a custom control by inheriting from the ProgressBar class and overriding its OnPaint method to draw the progress bar with a custom color.

using System.Drawing;
using System.Windows.Forms;

namespace DevAppCode
{
    //c# color progress bar
    public class ProgressBarEx: ProgressBar
    {
        public ProgressBarEx()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
        }

        // c# progress bar color
        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);
        }
    }
}

First, We create a custom control called ProgressBarEx by inheriting from ProgressBar class, then we override the OnPaint method to draw the progress bar using a custom color.

Next, We use ProgressBarRenderer to draw the background of the progress bar.

Finally, Rebuild your project, then you can see then ProgressBarEx on the left side of Visual Studio.

c# color progress bar

Another way you want to change the color of progress bar in c# windows application by using the win32 library.

The first thing you can create a new windows forms application project, then drag three progress bar controls from your visual studio toolbox to your winform.

Next you can layout your UI as shown below to learn how to change progress bar color in c# windows application.

change color of progress bar in c#

Create a new class with the name ProgressBarColor to help you change progress bar style in c# then modify your code as shown below.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace AppSource
{
   //c# color progress bar
    public static class ProgressBarColor
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr w, IntPtr l);
        public static void SetState(this ProgressBar p, int state)
        {
            SendMessage(p.Handle, 1040, (IntPtr)state, IntPtr.Zero);
        }
    }
}

The DllImportAttribute attribute helps you call a function exported from an unmanaged DLL. As a minimum requirement, you must provide the name of the DLL containing the entry point.

Add a Form_Load event handler to your windows forms application.

private void Form1_Load(object sender, EventArgs e)
{
    ProgressBarColor.SetState(progressBar1, 2);
    ProgressBarColor.SetState(progressBar2, 1);
    ProgressBarColor.SetState(progressBar3, 3);
}

Next, add the click event handler to your Color button, then add the code to handle your button click event as shown below.

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

To play the demo, you can use Thread.Sleep method to deplay process of your progress bar.

change color progress bar c#

Press F5 to run your application, then click the Color button you can change color of progress bar winform as shown above.

How to change the color of wpf progress bar in C#?

In WPF, the ProgressBar control provides more flexibility for styling through XAML. You can change the color by modifying the ControlTemplate or using a LinearGradientBrush.

For example how to update the color of a Progress Bar C# WPF

<ProgressBar Name="customProgressBar" Value="50" Maximum="100">
    <ProgressBar.Style>
        <Style TargetType="ProgressBar">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </ProgressBar.Style>
</ProgressBar>

First, We set the Foreground property of the ProgressBar to "Red" using a style, then you can adjust the color as needed by changing the value of the Foreground property.

You can update the color or progress value in your progress like this:

// c# wpf update
customProgressBar.Value = 70;

// Change color dynamically
customProgressBar.Foreground = new SolidColorBrush(Colors.Green);

These are simple examples to get you started with changing the color of progress bars in WinForms and WPF.