I need to change the color of the ProgressBar depending on the value I assign it. If the value is above 50, it should be green, if it's between 20 and 50, it should be yellow and if it's below 20, it should be red.
I found a great answer here on how to change the color of the ProgressBar. I tested it and it works. However it's written as an extension method.
public static class ModifyProgressBarColor
{
[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 pBar, int state)
{
SendMessage(pBar.Handle, 1040, (IntPtr)state, IntPtr.Zero);
}
}
I want the color to change when you set the value for the Value
property of the ProgressBar instead of having to manually assign it.
So I subclassed the ProgressBar class. Here's what I did.
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MyApp
{
class MyProgressBar: ProgressBar
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr w, IntPtr l);
public int Value
{
set
{
if (value > 50)
{
SendMessage(this.Handle, 1040, (IntPtr)1, IntPtr.Zero);
}
else if (value < 50 && value > 20)
{
SendMessage(this.Handle, 1040, (IntPtr)2, IntPtr.Zero);
}
else if (value < 20)
{
SendMessage(this.Handle, 1040, (IntPtr)3, IntPtr.Zero);
}
}
}
}
}
The control shows up in the Toolbox.
But when I set the Value
in code, nothing shows up in the ProgressBar!
Any idea what I'm missing here?
public int Value
should give you a Warning about hiding a base class member. Therein lies your answer. – Henk Holtermanpublic override int Value
but now I get the error Cannot override inherited member because it is not marked virtual abstract or override. I guess at this point, there's nothing that can be done? – IsuruValue
. Makes it much clearer what's going on. – Henk Holterman