Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-01-27
1
Medium Priority
?
1,287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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