Reading colors from .config file

  • Thread starter Thread starter Lonifasiko
  • Start date Start date
L

Lonifasiko

I want to have an entry like this in my .exe.config file:

<AppSettings>
<add key="EmergencyFormColor" value="Color.Red" />
<add key="MainFormColor" value="Color.AliceBlue" />
</AppSettings>

I'm able to read this parameters from code with OpenNETCF.Configuration
namespace but I obtain string datatype. In order to assign the
BackColor property of the form, I need the type to be
System.Drawing.Color but I can't find a way to convert the value!

Another option: Instead of Color.Red, write 12,12,12, that is, RGB
values. But by code I would have to split the string in three parts,
and convert the values into integer in order to use Color.FromARGB(int
R, int G, int B).

Do you see any direct converion or way I can do this easily? I want the
user to be able to change application's color easily.

Thanks.
 
You can split the app.config entry in 3 and load each. As in:
<AppSettings>
<add key="EmergencyFormColorR" value="0" />
<add key="EmergencyFormColorG" value="0" />
<add key="EmergencyFormColorB" value="0" />
</AppSettings>

Or split values with comas and then call string.split so you get an array of
strings which you can convert to RGB
 
Yes, I'm already working with the last solution you provide, it was my
initial thought.

Indeed, I don't see any possible easy and rapid solution.

Thanks anyway.
 
See an OpenNETCF.Drawing.ColorTranslator.FromHtml method. The method can
recognize the following color representations:

a) #009900 - #RRGGBB
b) Green - Color.XXXX

HTH
 
Awesome Sergey!

txt1.ForeColor = OpenNETCF.Drawing.ColorTranslator.FromHtml("Navy")
works great!

Only one thing, seeing your example (perhaps I've understood wrong),
I've also tried the following:

txt1.ForeColor =
OpenNETCF.Drawing.ColorTranslator.FromHtml("Color.Navy") --> This
throws a "cannot convert color name" exception.

Just to let you know.

Once again, OpenNETCF more complete than the rest!

Thanks very much.

Regards.
 
You don't need to write "Color." since FromHtml uses Reflection to
retrieve a property from Color class. The Color class has the "Navy"
propery but there is no "Color.Navy" property.
 
Back
Top