By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,557 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,557 IT Pros & Developers. It's quick & easy.

Serializing a Color property

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Serialize/Deserialize it as in integer using Color.ToArgb and Color.FromArgb
methods.

http://tinyurl.com/5odsm

hope that helps..
Imran.

"Tom" <to*@nospam.com> wrote in message
news:eU****************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #2

P: n/a
I was playing with the code yesterday:
http://msdn.microsoft.com/vbasic/usi...et07082003.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
"Tom" <to*@nospam.com> wrote in message
news:eU****************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:Os**************@tk2msftngp13.phx.gbl...
I was playing with the code yesterday:
http://msdn.microsoft.com/vbasic/usi...et07082003.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
"Tom" <to*@nospam.com> wrote in message
news:eU****************@TK2MSFTNGP10.phx.gbl...
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


Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.