Beginning ActiveX - Part 1, Page 6
When does an ActiveX control keep his properties? In his property bag!
No, seriously! You use the PropertyBag object to store values between sessions, a process known as persisting.
The PropertyBag object sports two methods: one to write values into the bag, and another one to retrieve them. You should write properties to the bag whenever one changes. This is done using the WriteProperties event of the usercontrol. This provides an instance of the PropertyBag object, which you can use to save the value. For example:
PropBag.WriteProperty "EyeOpen", m_EyeOpen, True
This saves the property, under the name of "EyeOpen". The third parameter, in this case set to True, is the default. Use this in conjunction with the default parameter of the ReadProperties method to set a default value. If the property is equal to this value, then it is not actually saved. When it comes to reading the value, the ReadProperty function sees that there is not value in the bag, so returns the default value. This provides a way to reduce the amount of memory required to persist the object.
When the control is restarted, you must reload all the saved values. The ReadProperties event is fired when this must be done. In this event, you must do the opposite to what you did in the WriteProperties event., You should resort all of the values that you stored, making sure that the default values are the same. For example:
m_EyeOpen = PropBag.ReadProperty("EyeOpen", True)
One way to synchronise the default values is to create a constant in the declarations section, so that you can reference it anywhere you need to. For example:
Private Const m_def_EyeOpen = True
Again, the 'm_def_' prefix is used generally for a default value constant.
To make a property have a default value at startup, use the InitProperties event of the usercontrol. This event is only triggered when the control is first placed on a form, then never again at design time. You would probably want something similar to this to achieve the desired effect:
Private Sub UserControl_InitProperties() m_EyeOpen = m_def_EyeOpen End Sub
The last thing to do is to tell the control that your property has changed so that it can save it into the PropertyBag. You do this by calling the "PropertyChanged" method in the Property Let procedure:
Now that I have opened your eyes ('scuse the pun!) to properties, lets move on to the next type of member: methods...