Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello, Here I did 2 codes to change the back colour of the panel. Here I am getting the colour value form 1st line is(stuffitblue) which is defined in Style in JSON formate come form server.
It showing error in last line.

My first code:
C#
dynamic stl7= obj7["Style"];
string color= stl7.ToString();
ColorConverter colConvert = new ColorConverter();
panelitemname.BackColor = (Color)colConvert.ConvertFromString(color);

My second code:
C#
dynamic stl7= obj7["Style"];
panelitemname.BackColor = stl7;

Please give any code if someone have. Thanks.
Posted
Updated 1-Apr-14 3:32am
v3
Comments
On which line?
Priyanka Bhawsar 1-Apr-14 9:24am    
Showing error on last line in both condition.

You can build your own mapper to convert color names into color co-ordinates.
Then assigning the color names would translate into the appropriate values.
 
Share this answer
 
You can try it :
C#
dynamic stl7= obj7["Style"];
//Creates a Color structure from the specified name of a predefined color.
Color clr = = System.Drawing.Color.FromName(st17.ToString());
panelitemname.BackColor = clr;

You can read more about System.Drawing.Color.FromName here:
http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx[^]

Good luck!
 
Share this answer
 
Comments
Priyanka Bhawsar 2-Apr-14 1:03am    
Thanks to answer me, this code is not showing any error but also not showing any color.
lukeer 2-Apr-14 5:10am    
That's because calling Color.FromName()[^] with an unknown colour name like "stuffitblue", causes it to return fully transparent black (#00000000).
Priyanka Bhawsar 2-Apr-14 7:29am    
Yes, so how to give it that colour ?
Volynsky Alex 2-Apr-14 7:25am    
Yes
First one works on my machine. At least if I set color to "Blue". Since you mention "stuffitblue", that colour is not defined in System.Drawing.Color.

You can use this code as check for colour name validity.

Or use this:
C#
Color color = (Color)Enum.Parse(typeof(KnownColor), "stuffitblue");


But for conversion, you can omit the ColorConverter altogether and use
C#
Color color = Color.FromName(st17.ToString());


For colours defined by a 32-bit value, you can use the FromArgb method:
C#
int jsonColourValue;
// Get value "2A68A2" from stuffitblue
Color color = Color.FromArgb(jsonColourValue);
 
Share this answer
 
v3
Comments
Priyanka Bhawsar 2-Apr-14 1:01am    
Thanks to answer me but here stuffitblue is a css class in which I defined the background:#2A68A2;.I am getting the stuffitblue in json value. I am sending that JSON value.

{
"Formatted Name": "Samsung",
"Style": "stuffitblue",
"Supports": [{
"Name": "Follow",
"URL": "IOGLO000030000900004000430000100004.Link?aLink_GUID=29b1468f-c8c3-db23-3097-c85369f818cd&aOutputFormat=JSON"
}],
"Type": "Button"
},
lukeer 2-Apr-14 5:10am    
Then, you need to somehow get that "stuffitblue" to tell you "2A68A2". I updated my solution accordingly.
But how exactly you would get the number from the style, I have no clue.
Priyanka Bhawsar 2-Apr-14 7:28am    
NO, its showing an error :Cannot implicitly convert type 'string' to 'int'.
lukeer 3-Apr-14 2:21am    
Just in case this is still relevant to you:
Mark the exact line of code where this error appears. Preferably do that by Improving you question, including <pre> tags around code, and then leave a reply on this comment so I get a notification.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900