How to set the backcolor of a control


The following statement sets the BackColor to a named color.
celHole2.BackColor = System.Drawing.Color.Green;

How do I set the BackColor to a Hex color? ie. Hex={00,99,33} RGB

celHole2.BackColor = System.Drawing.Color.{00,99,33} RGB;

Thanks,
DovbermanAsked:
Who is Participating?
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
Color.FromArgb(int red, int green, int blue)
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
And
Convert.ToInt16("80", 16);  //where the string part is the hex value
0
 
Pratima PharandeConnect With a Mentor Commented:
System.Drawing.Color.FromArgb(00,99,33)
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
AndyAinscowFreelance programmer / ConsultantCommented:
Is something not clear?


eg.
this.listBox1.BackColor = Color.FromArgb( Convert.ToInt16("FF", 16), Convert.ToInt16("00", 16), Convert.ToInt16("00", 16));

or this with your colour

this.listBox1.BackColor = Color.FromArgb( Convert.ToInt16("00", 16), Convert.ToInt16("99", 16), Convert.ToInt16("33", 16));
0
 
DovbermanAuthor Commented:
Thank you.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Don't forget - the comment from pratima_mcs doesn't actually work for what you requested (hex values for the colours).  Try it with another valid hex value
System.Drawing.Color.FromArgb(FF,99,33) and it won't even compile.
0
 
DovbermanAuthor Commented:
I used celHole1.BackColor = System.Drawing.Color.FromArgb(00, 99, 33);

This works.

I did not notice the System.Drawing.Color.FromArgb(FF,99,33) statement from Pratima MCS.

The statement from Pratima MCS that I used successfully was System.Drawing.Color.FromArgb(00, 99, 33);

If I miscalculated the credits, please ask the monitor to make the correction.

Thank you,
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
>>celHole1.BackColor = System.Drawing.Color.FromArgb(00, 99, 33);

That is NOT using those hex values you specified, they are the int values.  To use those hex values you must convert them to integer values - see my comments.


credits - I'll leave it in your hands if you want to ask a moderator to change things.  I posted my later comment in case you didn't realise the mistake that a hex value of 99 is not the same as an integer value of 99.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.