Solved

Casting From String To Color in C#

Posted on 2004-03-31
3
1,910 Views
Last Modified: 2009-04-24
Hi to alll ...
I want to save color in regeistry as a text, But how could I convert it to Color object after retreiving the value from regustry ....
the following code doesn't work
Text.BackColor = ((Color)GetValue(....path.....))

thanks


0
Comment
Question by:programmer_bmb
  • 2
3 Comments
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10726638
Color is an enumeration and so you can get the corresponding color from the name using that syntax :

String s = "Blue"
Color c = (Color)Enum.Parse(typeof(Color), s);
0
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10726646
You may include that in a try catch block as an ArgumentException can be thrown if the provided string is not a correct Color
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 25 total points
ID: 10726788
Or use the System.Drawing.Color.FromName method, e.g.:

  System.Drawing.Color myColor;
  myColor = System.Drawing.Color.FromName("Blue") ?

Or save an ARGB value represented by an integer in the registry, instead of a string:

  System.Drawing.Color myColor;
  // Choose a color in your application
  // e.g.:
  myColor = System.Drawing.Color.Blue;
  int argbColor = myColor.ToArgb();
  // Save value of argbColor in registry.            

  // Load value from registry.
  int argbColor = _;
  System.Drawing.Color myColor;
  myColor = System.Drawing.Color.FromArgb(argbColor);
  //myColor is now the original color.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.NET 2008 VB and C# 6 39
c# - Best approach for objects in functions 3 24
abstract class C# 1 31
Where does legacy ASP.NET initialize language? 2 22
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question