How to get the Color value from SystemColors

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
F

Frank Rizzo

I am being passed a string, such as ControlText. From this string, I
must figure its color value and set that value to a control.

All these values are stored in the System.Drawing.SystemColors.* enums.
I've tried to pry it open with Enum.Parse, but no dice, it keeps giving
me exceptions of all kinds.

Has anyone else figured out how to get the value of the Enum from a
string form?

Thanks.
 
Frank said:
I am being passed a string, such as ControlText. From this string, I
must figure its color value and set that value to a control.

All these values are stored in the System.Drawing.SystemColors.* enums.
I've tried to pry it open with Enum.Parse, but no dice, it keeps giving
me exceptions of all kinds.

Has anyone else figured out how to get the value of the Enum from a
string form?

Thanks.

I just looked at it again and all the Colors in both SystemColors and
Color classes are actually properties. So, the question is how to get a
property based on the name of the property.
 
You can use Color.FromName("ControlText") to get the Color object associated
with that system color. In my pc, for instance, that returns a Color object
with R=0, G=0, B=0.

Here's an interesting article about a security flaw related to using
Color.FromName, though. This article rightly suggests using a TypeConverter
to extract a Color from a string.

http://www.nikhilk.net/CrossSiteScriptingAttackWithColors.aspx

If that doesn't help, and you still want to get the value of a property
based on a string name of the property, use the
System.Reflection.PropertyInfo class. Here's an example from a recent
project of mine that may help you get started. It is from a control used in
an asp.net page. It looks for a property named CurrentServer of type
RequestedServer in the page that contains the control that this code is
from.

Type pageType = (Page.GetType());

PropertyInfo myPropertyInfo = pageType.GetProperty("CurrentServer");

if (myPropertyInfo != null)

{

requestedServer = (RequestedServer)myPropertyInfo.GetValue(Page,null);

}


Good luck,

DalePres
MCAD, MCSE, MCDBA
 
Back
Top