Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1530

[vb6] PropertyBag, Persisting, Cloning for UserControls

$
0
0
With VB, we have the use of the PropertyBag object. It is used in usercontrols to persist settings and can be used in public classes (within DLL,OCX projects) that have the Persistable property set to true.

What is shown below is nothing new, but maybe just a different way of doing it, from a UserControl perspective.

The usercontrol, and maybe some classes that you create, have a ReadProperties and WriteProperties event. These events are called when the object is being created (InitProperties,ReadProperties) and destroyed/saved (WriteProperties).

In your usercontrol project, you may have several classes also. Some of these classes have data that needs to be saved to the usercontrol. When the usercontrol is loaded, the class is created and that data loaded into the class. By using a property bag, whether your class has the Persistable property set to true or not, you can persist the data. Additionally, the user can have the option to "export" the class data so they can save it where/when they wish. The user could also "import" the data into the class from a previously saved state.

In my hypothetical usercontrol (UCWidget), I have several classes, some of which the user can create, modify, then assign to UCWidget via properties. One such hypothetical class is IAttributes. The IAttributes class is accessed via UCWidget.Attributes property directly during runtime and indirectly during design-time via the property page.

The IAttributes class, along with all classes to be persisted, within the UCWidget, project has an Export and Import sub. That sub is either Public or Friend declared. Public versions allow importing/exporting by both the UCWidget control and/or the user. The Friend declared versions only allow the UCWidget to do the import/export. The functions look like this:
Code:

Public Sub Export(PropBag As PropertyBag)
    If PropBag Is Nothing Then Set PropBag = New PropertyBag
    ' save all class settings to the property bag, exactly same as a usercontrol's WriteProperties event
End Sub

Public Sub Import(PropBag As PropertyBag)
    If PropBag Is Nothing Then
        ' optional: reset all properties to default, exactly same as a usercontrol's InitProperties event
    Else
        ' read all class settings from the property bag, exactly same as a usercontrol's ReadProperties event
    End If
End Sub

A user, during runtime, can import/export via a property bag:
Code:

' export example
    Dim myBag As PropertyBag
    UCWidget1.Attributes.Export myBag ' export the IAttributes class data
    ' save myBag.Contents to file. Contents can be assigned to a  byte array if desired

' import example
    Dim myBag As PropertyBag
    ' read previously saved data to array: byteData() As Byte
    Set myBag = New PropertyBag
    myBag.Contents = byteData(): Erase byteData()
    UCWidget1.Attributes.Import myBag ' import data into the IAttributes class

Likewise, the UCWidget control itself can serialize IAttributes during its Read/WriteProperties event:
Code:

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Dim pBag As PropertyBag
    m_Attributes.Export pBag ' m_Attributes is the UCWidget's class instance of IAttributes
    PropBag.Write "Attrs", pBag.Contents
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    Dim pBag As PropertyBag
    Set pBag = New PropertyBag
    pBag.Contents = PropBag.ReadProperty("Attrs")
    m_Attributes.Import pBag ' m_Attributes is the UCWidget's class instance of IAttributes
    ' m_Attributes set to new instance during UserControl_Initialize
End Sub

The same approach can be used to clone anything in your usercontrol project. For example, cloning an IAttributes class and assigning it to a different UCWidget control.
Code:

    Dim pBag As PropertyBag
    UCWidgetA.Attributes.Export pBag
    UCWidgetB.Attributes.Import pBag

We can even clone the entire UCWidget to another UCWidget control. But we'd first want to expose the Read/WriteProperties events via a public Export/Import subroutine:
Code:

Public Sub Export(PropBag As PropertyBag)
    If PropBag Is Nothing Then Set PropBag = New PropertyBag
    ' save all class settings to the property bag, exactly same as a usercontrol's WriteProperties event
End Sub
Public Sub Import(PropBag As PropertyBag)
  ' create any classes/references needed for this object (code usually found in the UserControl_Initialize event)
    If PropBag Is Nothing Then
        ' reset all properties to default, exactly same as a usercontrol's InitProperties event
    Else
        ' read all class settings from the property bag, exactly same as a usercontrol's ReadProperties event
    End If
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call Me.Export(PropBag) ' redirect so we don't need double the code
End Sub
Private Sub UserControl_InitProperties()
    Call Me.Import(Nothing) ' redirect so we don't need double the code
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    Call Me.Import(PropBag) ' redirect so we don't need double the code
End Sub


Viewing all articles
Browse latest Browse all 1530

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>