Solved

Casting From String To Color in C#

Posted on 2004-03-31
3
1,883 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now