1

I created a label and need to change the color of it programmatically to a specific RGB color code, if a certain event triggers.

I tried it like this:

statusStripTestLabel.ForeColor = new Color(128, 64, 64);

BONUS:

Set it to a RGBA color.

But I get Color does not have a constructor which accepts 3 arguments.

So how do I solve this?

Black
  • 18,150
  • 39
  • 158
  • 271

2 Answers2

4
statusStripTestLabel.ForeColor = Color.FromArgb(128, 64, 64); //Alpha is implicitly 255.

statusStripTestLabel.ForeColor = Color.FromArgb(255, 128, 64, 64); //Alpha is explicitly 255. You can change the value of the first parameter to specify the alpha you want.
Daniel Marques
  • 683
  • 8
  • 17
  • Missed an argument there. – marsze Aug 08 '17 at 12:48
  • 1
    Indeed, just add another value for A (or Alpha to indicate Opacity) as 255 so that reads as Color.FromArgb(255, 128, 64, 64); – RoguePlanetoid Aug 08 '17 at 12:50
  • Actually, I didn't. The method FromArgb() has 4 overloads. If you want to specify the alpha, you can use the overload that accepts 4 arguments. Since the question didn't bother to specify the alpha, he can use the overload that accepts the 3 arguments above (int red, int green, int blue). – Daniel Marques Aug 08 '17 at 12:51
  • Alpha is implicitly 255 when you use the overload above. – Daniel Marques Aug 08 '17 at 12:52
0

What u are trying is somewhat right in JAVA. In c# it is,

yourComponent. ForeColor=Color. FromArgb(theARGB_Code) ;

*Note not all components support transparency

Kabeer
  • 46
  • 9