Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to serialize standard properties of vb control

Posted on 2014-12-03
4
Medium Priority
?
206 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
[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
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
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…

688 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