Creating Classes & ActiveX Controls

Saving and reading properties

So far, if you add your control to a form, set some properties, and then close the form design window, those properties will be lost. When you re-open that window and look at the controls properties again, you will find that none of them have been saved. In order to do this, you need to use two procedures:

'// Load property values from storage
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

End Sub

'// Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)

End Sub

Then, for each property you need to save its value in the WriteProperties event, and read them in the ReadProperties event. You do this by calling the PropBag.WriteProperty and PropBack.ReadProperty methods. They use the following syntax:

'// a function - returns the property value
'// parameters in [] are optional
ReadProperty(Name As String, [DefaultValue])

WriteProperty(Name As String, Value, [DefaultValue])

So, as an example, the following code allows VB to read and write the Text property:

'// Load property values from storage
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    '// fill the private variable (m_Text) with the value of Text. You can specify a default value, in this case the constant m_def_Text
    '// first param: Property Name
    '// second optional param: Default Value
    m_Text = PropBag.ReadProperty("Text", m_def_Text)
End Sub

'// Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    '// save the private m_Text variable. You can specify a default value, in this case m_def_Text
    '// first param: Property Name
    '// second param: Property Value
    '// third optional param: Default Value
    Call PropBag.WriteProperty("Text", m_Text, m_def_Text)
End Sub

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC. As potential programmers, they are mentally mutilated beyond hope of regeneration.” - E. W. Dijkstra