Solved

how to serialize standard properties of vb control

Posted on 2014-12-03
4
176 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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