473,394 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Serializing a Color property

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
3 3547
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dotNetDave | last post by:
I'm trying to create xml seriaizable collection class (below), but the xml keeps coming out wrong. In the resulting xml from the web service (below) the "ArrayOfAlarmProcessor" tag should really be...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
6
by: Rein Petersen | last post by:
Hi Folks! Here's a strange behaviour: Without a properties SET accessor (see code below), the property will not serialize. public class myObject {
1
by: Chris | last post by:
I'm having trouble Serializing a System.Data.DataColumn object. When I try to serialize it, I get the following: System.NotSupportedException: Cannot serialize member...
2
by: Doug Eller | last post by:
I am serializing datasets and sending them out to a client application via a web service. My problem is that the associated schema includes all of my connection string, database username and...
3
by: axr | last post by:
Having trouble with Serilization of objects that contain members which are of type Interface eg public class SomeClass { ISomeInterface1 itf1; ClassType1 ct1; ISomeInterface2 itf2;
2
by: Charles Law | last post by:
I have a complex object that I am serializing, and I would like to omit elements that have a default value. For example, if I have a class as follows Public Class Test Private m_Name As...
7
by: fjlaga | last post by:
I have written an Office Add-in for Excel using VB.NET and the .NET 1.1 Framework (I have Visual Studio 2003 .NET ). All works great. I want to add a User Settings/Prefereneces dialog and allow...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.