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
Solved

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

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

792 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