Read Color from XML file

Dodgeram01

Freshman
Joined
Apr 9, 2002
Messages
41
Location
Upstate NY
Is there a way to save the back color of a textbox in the same format everytime? Sometimes it will save the color as
Code:
Color [Red]
And sometimes it will save it as
Code:
Color [A=255, R=82, G=226, B=180]
.
I need to be able to read this string and convert it to the type system.drawing.color, which I can do. My problem is I can't do this if I don't know what format the color will save in. Is there any easy way around this, or will I have to play around with seeing what format it is in and then formatting it how I need it?
 
You can use FromArgb and ToArgb....
Visual Basic:
Me.BackColor = Color.FromArgb(255, 0, 0)'simulate a color change

Dim x As Color = Me.BackColor ' get the color value of a control into x
MessageBox.Show(x.ToArgb.ToString)' use this to save the value as a string
textbox1.BackColor = x 'since x is already of type Color, we can assign it to a control

[edit]
'to pu it simply, use this method to save the color value...
Me.BackColor.ToArgb.ToString()
[end Edit}
 
If you're serializing and deserializing the textbox using the
standard XML serialization classes, then I would think that the
deserializer would handle all this for you.

But to answer your question, you can use two methods of the Color
class to get the color. If the string contains a number, then it
must be ARGB values, and you can parse out the color value and
create a color from the Color.FromArgb() method.

If there aren't any numbers, then it must be a named color, and
the Color.FromName() method will retrieve the correct color based
on the known color name ("Red" in this case).
 
Back
Top