Using LinearGradientBrush
-
I have a figure in a winform, a rectangle, and a NumericUpDown, its miniumum and maximun values are 0 and 100, i want that when i increase the NumericUpDown value the rectangle must change its color, for example when the value is 0 the rectangle color must be black and when i increase the value the rectangle color must turning into white, when the value is 100 the rectangle color must be completely white. I have seen some examples using LinearGradientBrush but i don't know exactly how to do that, somebody knows how could i do this? Regards, Alberto Martinez
-
I have a figure in a winform, a rectangle, and a NumericUpDown, its miniumum and maximun values are 0 and 100, i want that when i increase the NumericUpDown value the rectangle must change its color, for example when the value is 0 the rectangle color must be black and when i increase the value the rectangle color must turning into white, when the value is 100 the rectangle color must be completely white. I have seen some examples using LinearGradientBrush but i don't know exactly how to do that, somebody knows how could i do this? Regards, Alberto Martinez
If you want a solid colour which it looks like you do then you should use SolidBrush. In the Change / Click event of the NumericUpDown invalidate the region where you're drawing the rectangle. In the paint event handler do something like:
using (SolidBrush brush = new SolidBrush((byte)(numericUpDown.Value * (100 / 255))...
{
graphics.FillRectangle(...)
}basically you need to scale 0-100 up to 0-255 for each red, green and blue component of the colour. Regards Ed
-
If you want a solid colour which it looks like you do then you should use SolidBrush. In the Change / Click event of the NumericUpDown invalidate the region where you're drawing the rectangle. In the paint event handler do something like:
using (SolidBrush brush = new SolidBrush((byte)(numericUpDown.Value * (100 / 255))...
{
graphics.FillRectangle(...)
}basically you need to scale 0-100 up to 0-255 for each red, green and blue component of the colour. Regards Ed
Sorry to interrupt, but the code above will not work. If your write (100 / 255) you'll get integer division resulting in 0, so the value of the numericUpDown won't have an effect for the color of the brush. Besides, to get values between 0 and 255 you'll have to reverse the fraction. So something like
(int)(numericUpDown.Value*255/100)
should work better. Regards, mav -- Black holes are the places where god divided by 0... -
Sorry to interrupt, but the code above will not work. If your write (100 / 255) you'll get integer division resulting in 0, so the value of the numericUpDown won't have an effect for the color of the brush. Besides, to get values between 0 and 255 you'll have to reverse the fraction. So something like
(int)(numericUpDown.Value*255/100)
should work better. Regards, mav -- Black holes are the places where god divided by 0... -
I was thinking I needed more sleep and you just confirmed it :) I got my fraction upside down :omg: Ed
Hi, Thanks both of you for your help i already could do what i needed :-D Regards, Alberto Martinez