Solved

Casting From String To Color in C#

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

861 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

23 Experts available now in Live!

Get 1:1 Help Now