Library tutorials & articles

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

Comments

  1. 18 Jan 2005 at 10:56

    Is it possible for a dll to use an ocx and its methods? How do I declare it?

  2. 04 Nov 2004 at 05:57

    thanks!!! a great help for developers.










    --all glory to my great provider...--

  3. 13 Apr 2003 at 16:12

    Of all the books and tutorials I've read on classes & activeX,this should be the best.Thanx to the author.

  4. 28 Feb 2002 at 16:39

    I am fairly new at coding in VB and love diving in.  I am working on a project on reading binary files, parsing bit data information and creating a display from some function of bits.  The first couple of words will tell me how to set up a window and other parameters.  Since my first run at creating this program, I have since discovered the use of classes as separate modules.  I'm trying to create a couple of classes, one for setting of the parameters and the other for program settings.  Using the Property of Let & Get has worked great and makes the management of what I am doing easier.  But I have a couple of questions regarding of how to try to pass arrays like I would the other values.  
     For example, recalling a list of last opened file names and making them available to other modules, I had wanted to try to put it in a class like the other properties, but when I don't know if and how many how do I do that?  
     

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Creating Classes & ActiveX Controls.

Leave a comment

Sign in or Join us (it's free).