Save (serialize?) some properties

M

Marco Trapanese

Hello,

I'm sorry if my English is not so good.

I created a usercontrol composed of a picturebox and a label. I also
added few properties to configure its behavior.
In the main form there are a dozen of them, the final user can customize
them through those few properties.

The goal is to save in a file such a configuration (that is the values
of 4-5 properties) in order to recall when needed.

I tried to serialize (binary, soap, xml formatters) the whole
usercontrol and also only the properties I need but without chance.
I got different errors even if I followed several guides found on the web.

So I'd ask you if you can provide me a very short example of "how to
serialize a usercontrol".

I did something like this:

<Serializable()> Public Class IOItem
Inherits UserControl

Public Enum Modes
Button
Toggle
Input
End Enum

Dim _Mode As Modes
Dim _LabelColor As Color
Dim _LabelText As String
Dim _LabelFont As Font
Dim _Config As Boolean = False
Dim _Value As Boolean = False

Public Property Mode() As Modes
Get
Return _Mode
End Get
Set(ByVal value As Modes)
_Mode = value
Update()
End Set
End Property

....
other properties and methods
End Class

In the main form I placed some of this user controls and in the load
event I aggregated them into a generic list (for easy manipulation):

Dim IOItemArray As New List(Of IOItem)

IOItemArray.Add(IoItem1)
IOItemArray.Add(IoItem2)
IOItemArray.Add(IoItem3)
..
..
..

The save sub should do this:

Dim fs As FileStream = New FileStream(diaSaveCfg.FileName, FileMode.Create)
Dim xf As New XmlSerializer(IOItemArray.GetType)

Try
xf.Serialize(fs, IOItemArray)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
fs.Close()
End Try


In this case I get the following message (copy & paste)

"There was an error reflecting type
'System.Collections.Generic.List`1[IO_Automation.IOItem]'."

Using binary or soap formatters I get other error messages but all of
them tells me I can't serialize my user control.

What's wrong?

Thanks
Marco
 
J

James [hotmail]

found this piece of code in a book:

dim xml_serializer as new xmlserializer(gettype(OrderItemClass))
dim file_stream as new filestream(m_xmlfile, filemode.create)
xml_serializer.serialize(file_stream, OrderItemClass)
file_stream.close

check the contents of this file should be an xml file. the Key is setting
the serializer to GetType(OrderItemClass) or whatever your type/class is
called.


in some example they have variables maked like this:
<XmlAttributeAttribute(AttributeName:="ThisName")> public Item as string

looks as though this will save the value for Item with the name "ThisName"
and when read will place the value back into OrderItemClass.Item.
 
O

OmegaSquared

Hello, Marco,

Your English is fine. No apologies are needed.

I seem to recall that Controls are non-serializable. There may be ways
around this by doing some form of custom serialization, but I think that
creating an intermediary class that is serializable is probably the easiest
solution.

Here is an example that does this for a TreeView control:

http://www.codeproject.com/KB/vb/TreeViewDataAccess.aspx

I hope that it gives you some ideas.

Cheers,
Randy
 
M

Marco Trapanese

OmegaSquared ha scritto:
but I think that
creating an intermediary class that is serializable is probably the easiest
solution.



Thank you for your suggestion. I created a simple structure with the
data I want to save. Serialize and deserialize it is pretty easy as well
as bind its values to the related control.

Marco
 
J

James [hotmail]

Hello,
I am having the same error - the only thing we have in common is the use of
the array. I used dictionary(string, myClass). Suspect these structures are
not well reflective.


OmegaSquared said:
Hello, Marco,

Your English is fine. No apologies are needed.

I seem to recall that Controls are non-serializable. There may be ways
around this by doing some form of custom serialization, but I think that
creating an intermediary class that is serializable is probably the
easiest
solution.

Here is an example that does this for a TreeView control:

http://www.codeproject.com/KB/vb/TreeViewDataAccess.aspx

I hope that it gives you some ideas.

Cheers,
Randy


Marco Trapanese said:
Hello,

I'm sorry if my English is not so good.

I created a usercontrol composed of a picturebox and a label. I also
added few properties to configure its behavior.
In the main form there are a dozen of them, the final user can customize
them through those few properties.

The goal is to save in a file such a configuration (that is the values
of 4-5 properties) in order to recall when needed.

I tried to serialize (binary, soap, xml formatters) the whole
usercontrol and also only the properties I need but without chance.
I got different errors even if I followed several guides found on the
web.

So I'd ask you if you can provide me a very short example of "how to
serialize a usercontrol".

I did something like this:

<Serializable()> Public Class IOItem
Inherits UserControl

Public Enum Modes
Button
Toggle
Input
End Enum

Dim _Mode As Modes
Dim _LabelColor As Color
Dim _LabelText As String
Dim _LabelFont As Font
Dim _Config As Boolean = False
Dim _Value As Boolean = False

Public Property Mode() As Modes
Get
Return _Mode
End Get
Set(ByVal value As Modes)
_Mode = value
Update()
End Set
End Property

....
other properties and methods
End Class

In the main form I placed some of this user controls and in the load
event I aggregated them into a generic list (for easy manipulation):

Dim IOItemArray As New List(Of IOItem)

IOItemArray.Add(IoItem1)
IOItemArray.Add(IoItem2)
IOItemArray.Add(IoItem3)
..
..
..

The save sub should do this:

Dim fs As FileStream = New FileStream(diaSaveCfg.FileName,
FileMode.Create)
Dim xf As New XmlSerializer(IOItemArray.GetType)

Try
xf.Serialize(fs, IOItemArray)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
fs.Close()
End Try


In this case I get the following message (copy & paste)

"There was an error reflecting type
'System.Collections.Generic.List`1[IO_Automation.IOItem]'."

Using binary or soap formatters I get other error messages but all of
them tells me I can't serialize my user control.

What's wrong?

Thanks
Marco
 
Top