How to Change color of Progress Bar in C#
By FoxLearn 7/18/2024 7:35:28 AM 19.31K
Here's how you can change the color of a progress bar in c# (WinForms or WPF)
How to change the color of progressbar in C# WinForms
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); } } }
In this code:
- We create a custom control called ProgressBarEx
by inheriting from ProgressBar
class.
- We override the OnPaint
method to draw the progress bar using a custom color (Red in this case).
- We use ProgressBarRenderer
to draw the background of the progress bar.
Finally, Rebuild your project, then you can see ProgressBarEx on the left side of Visual Studio.
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.
Create a new class with the name ProgressBarColor to help you change progress bar style in c# then modify your code as the following.
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.
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 progressbar in C# WPF
In WPF, you can customize the appearance of a progress bar using XAML and styles. Here's a basic example of changing the color of a progress bar in WPF:
<ProgressBar Value="50" Maximum="100"> <ProgressBar.Style> <Style TargetType="ProgressBar"> <Setter Property="Foreground" Value="Red" /> </Style> </ProgressBar.Style> </ProgressBar>
In this XAML:
- We set the Foreground
property of the ProgressBar
to "Red" using a style.
- You can adjust the color as needed by changing the value of the Foreground
property.
These are simple examples to get you started with changing the color of progress bars in WinForms and WPF. You can further customize the appearance according to your requirements.