Serializing a Color property

T

Tom

I am having trouble serializing a color property. Basically, it is not
serializing the value. For instance, in the following:

<Serializable()> _
Public Class TestColor
Private _MyColor As System.Drawing.Color = System.Drawing.Color.Red

Public Property MyColor() As System.Drawing.Color
Get
Return _MyColor
End Get

Set(ByVal Value as System.Drawing.Color)
_Orange = Value
End Set
End Property
End Class

If I instantiate this class, set the MyColor property to
System.Drawing.Color.Purple and then serialize it to an XML file, I get
something like the following line in the file:

<MyColor />

which is obviously not correct. And when deserialized MyColor of course gets
set to nothing/White/0.

I know that I saw somewhere that there was -something- extra I had to do to
serialize/deserialize this type of data (i.e. Color data) properly. But I
can't remember what it was and can't seem to find it anymore. Anyone got the
solution? Driving me crazy....

Tom
rather than the actual
 
G

Greg Burns

I was playing with the code yesterday:
http://msdn.microsoft.com/vbasic/us...library/en-us/dnadvnet/html/vbnet07082003.asp

It serializes the color property differently for some reason I don't quite
get. Is this what you are looking for???

HTH,
Greg

Imports vbUserSettings

''' <summary>
''' Stores settings for Form1.
''' </summary>
Public Class Form1Settings
Inherits UserSettingsBase

''' <summary>
''' Indicates whether the form is currently round.
''' </summary>
Public IsRound As Boolean
''' <summary>
''' The form's Opacity percentage (0-100).
''' </summary>
Public Opacity As Double = 100
''' <summary>
''' The top coordinate of the form.
''' </summary>
Public Top As Integer
''' <summary>
''' The left coordinate of the form.
''' </summary>
Public Left As Integer
''' <summary>
''' The height of the form.
''' </summary>
Public Height As Integer
''' <summary>
''' The width of the form.
''' </summary>
Public Width As Integer
Private mColor As Color = Color.DarkGoldenrod

#Region " Constructors "

''' <summary>
''' Creates a new, empty settings object which will be
''' stored in isolated storage.
''' </summary>
Public Sub New()

End Sub

''' <summary>
''' Creates a new, empty settings object.
''' </summary>
''' <param name="StorageOption">The location to store the
settings.</param>
Public Sub New(ByVal StorageOption As UserStorageOption)

MyBase.New(StorageOption)

End Sub

#End Region

#Region " Color "

''' <summary>
''' The BackColor setting for the form.
''' </summary>
''' <remarks>
''' This property is the same as ColorName except that it
''' works with the Color type. Setting this property changes
''' ColorName and visa versa.
''' </remarks>
<Xml.Serialization.XmlIgnore()> _
Public Property Color() As Color
Get
Return mColor
End Get
Set(ByVal Value As Color)
mColor = Value
End Set
End Property

''' <summary>
''' String representation of the BackColor setting for
''' the form.
''' </summary>
''' <remarks>
''' This property is the same as Color except that it
''' works with String values. Setting this property changes
''' Color and visa versa.
''' </remarks>
Public Property ColorName() As String
Get
Return mColor.ToKnownColor.ToString
End Get
Set(ByVal Value As String)
mColor = Color.FromName(Value)
End Set
End Property

#End Region

End Class
 
T

Tom

Yep! All these suggestions (both yours and Imran's) did the trick. Just had
to figure out what type of color it was, then either save it as a name or as
a number. Now working perfectly.

Thanks again!

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top