The
ReadProperties event fires when a custom control is
reinstantiated at some point in the development cycle (the Project where it resides has been
retrieved and its container has been instantiated, the developer has just entered run mode from
the design mode, or the developer has just returned to design mode from run mode).
Notice that we said that ReadProperties fires when
the custom control is re-instantiated. We used this phrasing to purposely exclude
the case when the developer places an instance of the control on its container
for the first time from the Toolbox. For such first-time
instantiation, the ReadProperties event doesn't fire.
Instead, the InitProperties event fires (see
"Using the InitProperties Event to Set Default Starting Property Values").
The ReadProperties event, as its name implies, is the
event that you will use to restore the values of properties that have been kept
in the Property Bag. The Property
Bag appears in the ReadProperties event procedure
as a parameter named PropBag. You call
PropBag's ReadProperty method for each property
whose value you wish to restore, as in Listing 13.9.
Notice that the ReadProperty method takes two arguments:
the name of the property as a string and then a default value for the property (in case the
property's value has not been initialized in the Property Bag).
We store the results of each call to ReadProperties in
the appropriate variable or control property that implements the property within this control.
LISTING 13.9
USING THE READPROPERTIES EVENT PROCEDURE TO RESTORE PERSISTENT PROPERTY VALUES FROM THE
PROPERTY BAG
Private Sub UserControl_ReadProperties _
(PropBag As PropertyBag)
m_Celsius = PropBag.ReadProperty("Celsius",
30)
m_TemperatureDate = _
PropBag.ReadProperty _
("TemperatureDate", DateSerial(1997, 1, 1))
m_caption = PropBag.ReadProperty _
("Caption", Extender.Name)
BackColor = PropBag.ReadProperty _
("BackColor", Ambient.BackColor)
'make housekeeping adjustments
'to bring constituent controls
'into line with these property values
lblCaption.Caption = m_caption
End Sub