Hi
how can I change progressbar already on form color by code without create another one
Hi
how can I change progressbar already on form color by code without create another one
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Johnnie Barlow9 reputationPosted Nov 24, 2024, 9:20 PM EST
You can change the color of an existing progress bar on a form by setting its
ForeColorproperty in the code. Here's an example in C#:progressBar1.ForeColor = Color.Red; // Change to any color you wantThis will change the progress bar's color without creating a new one. Just make sure the color you set is compatible with the background color of the progress bar for good visibility.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 19, 2024, 1:04 AM EST
Hi Samony Me,
In C#, the default
ProgressBarcontrol does not provide a direct property to change its color. However, you can achieve this by overriding theOnPaintmethod or using a custom control. Below is a simple approach using theProgressBarcontrol with a custom drawing technique.First, you need to create a subclass of
ProgressBarand override theOnPaintmethod to customize the appearance:using System; using System.Drawing; using System.Windows.Forms; public class CustomProgressBar : ProgressBar { protected override void OnPaint(PaintEventArgs e) { // Draw the background e.Graphics.FillRectangle(Brushes.LightGray, this.ClientRectangle); // Draw the progress float percentage = (float)this.Value / this.Maximum; e.Graphics.FillRectangle(Brushes.Green, 0, 0, this.Width * percentage, this.Height); } }Then, replace your existing
ProgressBarin the form withCustomProgressBar. You can change the color by modifying theBrushes.Greento any color you prefer. This way, you can dynamically change the color of the progress bar without creating a new instance.To use it, simply instantiate
CustomProgressBarin your form and set its properties as needed.