开发者

How to serialize and deserialize a form?

How do I serialize and deserialize my main form (and its controls, subcontrols, their values, etc.)?


Edit, for clarification. Currently I am writing the current value of each control to an .INI file, one by one and reading it back when the program is next run. Every time I add a new control, I have to remember to update that save/load .INI code.

I just wondered if I can do it in one call, or a simple for loop iterating over all开发者_如何学编程 controls on the form.

Btw, I use only simple controls like edit box, radio button, combo box, checkd listbox, the most complex thing I have is a datagrid, but even that is not linked to a databse.


Accepted answer "can't. I will probably code my own, along the lines of ...

for each child control (recursivley)

 if conrol is editbox ...

 if control is radiobutton ...

 if ... etc
   write control name to .ini file
   write control "value" to .ini file

maybe later add left/top/height/width/enabled/visible, etc, bu tfor not th econtrol name an its "value" are enough (text, value, lines, checked?, etc)


There's no out-of-the-box support for serializing .NET forms and controls. Controls are not marked with the [Serializable] attribute.

I think most of the difficulty revolves around .NET controls that are really wrappers of native Win32 controls. Persisting the native state as XML, it seems, would be infeasible.

Perhaps someone has written a custom serializer; if not, you may need to roll your own.

Edit:

I found this discouraging accepted answer on experts exchange:

You have to implement ISerializable or IXmlSerializable in order to do something like that (depending on how do you want to serialize the form). It's not trivial.


Serializing the entire control sounds like a difficult proposition. However, if you want to serialize the data within that control, that's certainly possible, assuming you have it structured decently well.

For example, let's say you have a Person object with a List of Address for your AddressBook application:

Public Class Person
    Public Property PersonName As String
    Public Property PersonAge As Integer
    Public Property Addresses As New List(Of Address)()
End Class

Public Class Address
    Public Property StreetAddress As String
    Public Property City As String
    Public Property State As String
    Public Property Zip As String
End Class

If you bind this data to your form, you can easily serialize and deserialize it to and from XML. To serialize:

Imports System.Xml.Serialization  '<==you need to import this namespace '
'...'

Dim thisPerson As New Person()
Dim serializer = new XmlSerializer(thisPerson.GetType())
Using writer = XmlWriter.Create("thisPerson.xml"))
    serializer.Serialize(writer, thisPerson)
End Using 

To deserialize:

Dim thisPerson As New Person()
Dim serializer As New XmlSerializer(thisPerson.GetType())
Using reader = XmlReader.Create("thisPerson.xml")
    thisPerson = CType(serializer.Deserialize(reader),Person)
End Using

I learned about XML Serialization from this answer to a question I had previously asked.

Granted, if you're manually loading/extracting data from your forms, this isn't going to work. I good idea might be to encapusate your underlying data for a form in a class and then bind that class to the form. This way, you can easily serialize/deserialize the data in the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜