Solved

VB.NET: Serialize and Deserialize a UserControl's Data

Posted on 2013-01-27
1
1,172 Views
Last Modified: 2013-01-27
I am using the code below to show a dynamic form containing a UserControl which is encapsulated into a class. The class is to be serialized so that (a) the properties of usercontrol objects can be saved, (b) the class can undergo garbage collection once closed, and (c) when a user wants to modify the usercontrol the deserialize routine would be called and the usercontrol with saved properties appears.

It is not clear whether I can user many usercontrols in a class, selected by case, which would prevent using a separate class for each usercontrol.

Below is the code thus far:
    <Serializable()> Public Class MyUC1 'one user control per class 
        Public Sub New()
            Dim myform As New Form
            myform.Show()
            Dim mycntl As New MyUserControlLibrary.myuc1
            myform.Controls.Add(mycntl)
            Dim btn As New Button
            btn.text = "Apply"
            btn.Top = 300
            btn.Left = 10
            myform.Controls.Add(btn)
            AddHandler btn.Click, AddressOf Form1.serialize
        End Sub
    End Class

    Public Class Form1

        ....

        Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
            Dim cntl As New MyUC1
        End Sub
        Sub serialize()
            Dim cntl As New MyUC1
            Dim ser As New XmlSerializer(GetType(MyUC1))
            ' Open the destination file.
            Dim fs As New FileStream("C:\Leifuse\VBNet 2010\NXG Without ShapeContainer\superperform.xml", FileMode.Create)
            ' Serialize the object to the stream, enforcing the specified namespaces.
            ser.Serialize(fs, cntl)
            fs.Close()
        End Sub
        Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
            Dim deser As New XmlSerializer(GetType(MyUC1))
            Dim fs As New FileStream("C:\myuc1.xml", FileMode.Open)
            Dim cntl As MyUC1 = CType(deser.Deserialize(fs), MyUC1)
            fs.Close()
        End Sub
    End Class

Open in new window

0
Comment
Question by:lep1
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38824983
You can't serialize controls.

You can, however, serialize the DATA from those controls.  So you'd have to create a class that holds the relevant data about your usercontrol and serialize that instead.

When you want to deserialize, you first deserialize the data into your custom class, then you create an instance of your usercontrol and load that data into it.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now