Solved

how to serialize standard properties of vb control

Posted on 2014-12-03
4
175 Views
Last Modified: 2014-12-04
I made this class

<Serializable()> _
Public Class MyLabel
    Inherits Label
    Implements ISerializable

#Region "Initializers"
    Public Sub New()
    End Sub
#End Region

#Region " properties ..."
    Private e_myproperty As String = Nothing
    <Browsable(True)> _
    Public Property myproperty() As String
        Get
            Return e_myproperty
        End Get
        Set(ByVal value As String)
            e_myproperty = value
        End Set
    End Property
#End Region


#Region "Serialization"
    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        e_myproperty = info.GetString("myproperty")
    End Sub

    Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
        info.AddValue("myproperty", e_myproperty)
    End Sub
#End Region
End Class

Open in new window


My question would be that if it is possible to serialize the standard properties of the control like "visible" or "enabled"

I dont know why no matter what I do if change the values of the standard properties before serialize this class to a file, when I deserialize from file this properties has the initial values not the values that I put before saved to a file
0
Comment
Question by:altariamx2003
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40480751
Standard controls are not marked with the <Serializable> attribute, so if you want to do binary serialization of a control, then you're going to have to manually do so with the ISerializable that you are already using.
0
 

Author Comment

by:altariamx2003
ID: 40480865
thanks for answer kaufmed

Did you know how to add and serialize into my class a standard property like "visible"???
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40481004
As I mentioned, use the ISerializable interface.

e.g.

<Serializable()> _
Public Class MyLabel
    Inherits Label
    Implements ISerializable

#Region "Initializers"
    Public Sub New()
    End Sub
#End Region

#Region " properties ..."
    Private e_myproperty As String = Nothing
    <Browsable(True)> _
    Public Property myproperty() As String
        Get
            Return e_myproperty
        End Get
        Set(ByVal value As String)
            e_myproperty = value
        End Set
    End Property
#End Region


#Region "Serialization"
    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        e_myproperty = info.GetString("myproperty")
        Me.Text = info.GetString("Text")
    End Sub

    Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
        info.AddValue("myproperty", e_myproperty)
        info.AddValue("Text", Me.Text)
    End Sub
#End Region
End Class

Open in new window


In the above, you can see that I added in the Text property to be (de)serialized. I'd suggest that when you deserialize that you do a mapping of the properties from the deserialized object to the instance of the control that is on your form rather than doing a direct assignment. I think it will be easier to understand in terms of what you are doing.

e.g.

Dim s As New BinaryFormatter()

Using f As FileStream = File.Open("out.bin", FileMode.Open)
    Dim temp As MyLabel = DirectCast(s.Deserialize(f), MyLabel)

    Me.MyLabel1.Text = temp.Text    ' Map the Text property rather than assigning the control directly
End Using

Open in new window


You can do a direct assignment of the deserialized control, but you'd also have to remember to add the deserialized control to the form's Controls collection as well as removing the old control.
0
 

Author Comment

by:altariamx2003
ID: 40481400
It works kaufmed!!!!

Thanks a lot!!!!
0

Featured Post

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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