1

I am using windows progressbar in winform. It's minimum value is 20 and maximumm value is 120. I have to set threshold in the progressbar for value=60 such that if value remains below 60 then progressbar colour will be red else if value is more than 60 then progress bar colour should be green. And the most important thing is that I want to show this threshold value as a line in progessbar that should be visible whatever may be the colour of progressbar green or red. Does anybody have idea?? Your help will be appreciated.

Sachin Patil
  • 171
  • 1
  • 3
  • 10
  • 1
    you mean somthing like we see in my computer explorer for drive volumes – Shekhar_Pro Mar 05 '11 at 09:36
  • possible duplicate of [How to change the color of progressbar in C# .NET 3.5?](http://stackoverflow.com/questions/778678/how-to-change-the-color-of-progressbar-in-c-net-3-5) – Shekhar_Pro Mar 05 '11 at 09:46

2 Answers2

1

You can try this (However you won't get animation).

It a modified version of this answer with Threshold features added.

public class NewProgressBar : ProgressBar
{
    //This property takes the Threshold.
    public int Threshold{ get; set; }

    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;
            //Check value is greater that Threshold
            if(this.Value > Threshold)
            {
                e.Graphics.FillRectangle(Brushes.Green, 2, 2, rec.Width, rec.Height);
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.Red, 2, 2, rec.Width, rec.Height);
            }
            //This line should do that
            e.Graphics.DrawLine(Pens.Black,Threshold-1,2,Threshold-1,rec.Height);
    }
}

Now you can use it like this:

NewProgressBar p = new NewProgressBar();
//Set properties
//Set threshold
p.Threshold =  60;    
Community
  • 1
  • 1
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • thanks... for your effort but what I needed was on threshold it shoul also show a line for value 60 which should be visible whatever be the colour of the progressbar.. red or green... – Sachin Patil Mar 05 '11 at 10:19
  • @ Shekhar_Pro : Not working.. some problem with x and y co-ordiante values in DrawLine method.. still thanks.. – Sachin Patil Mar 05 '11 at 11:08
0

With the default ProgressBar implementation, you will not be able to completely achieve what you want.You would need to create a custom control.

If you are interested in changing the color only, you can achieve that by changing the ForeColor property when the percentage value changes.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176