Solved

how to serialize standard properties of vb control

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
thanks for answer kaufmed

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

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
It works kaufmed!!!!

Thanks a lot!!!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

7 Experts available now in Live!

Get 1:1 Help Now