How Can i Covert String to System.Drawing.Brush

How Can i Covert String to System.Drawing.Brush.

what i want to do is like this:
String  colorString = ConfigurationManager.AppSettings["ColorString "];
System.Drawing.Brush  brush;
how can i convert ColorString  to brush?
HanbingChengAsked:
Who is Participating?
 
Miguel OzConnect With a Mentor Software EngineerCommented:
Assuming is a SolidBrush:
http://msdn.microsoft.com/en-us/library/system.drawing.solidbrush.solidbrush.aspx

your code should look like:
Color customColor = Color.FromName(colorString);
System.Drawing.Brush  brush = new SolidBrush(customColor);

Note: your string must be a valid color name (http://msdn.microsoft.com/en-us/library/system.drawing.color.fromname.aspx )
0
 
himanshutCommented:
maybe by:

brush = (System.Drawing.Brush)colorString;
0
 
himanshutCommented:
Try this as we;
// best, using Color's static method Color red1 = Color.FromName("Red");  // using a ColorConverter TypeConverter tc1 = TypeDescriptor.GetConverter(typeof(Color)); // ..or.. TypeConverter tc2 = new ColorConverter(); Color red2 = (Color)tc.ConvertFromString("Red");  // using Reflection on Color or Brush Color red3 = (Color)typeof(Color).GetProperty("Red").GetValue(null, null);  // in WPF you can use a BrushConverter SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
HanbingChengAuthor Commented:
We can use like this:
Brush brush = new SolidBrush(Color.FromName("Red"));
0
 
HanbingChengAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for HanbingCheng's comment http:/Q_27417715.html#37142999

for the following reason:

find a solution by myself
0
 
Miguel OzSoftware EngineerCommented:
Your posted solution is the same as mine. You just did it in one and substitute colorString with "Red". My points please
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.