Solved

Casting From String To Color in C#

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
defining NULL or 0 10 50
The control parameters are not inputing the value when displaying in a report viewer 13 32
Please explain purpose of GZIP 4 59
Return array 3 20
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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