Solved

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

Posted on 2013-01-27
1
1,185 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SLMGR Switches Are Not Working On KMS Host 3 67
Get id from json Data with NewtonSoft 3 33
Tool Box 2 34
Get the selected ValueMember of Combobox 5 20
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

912 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

16 Experts available now in Live!

Get 1:1 Help Now