Visual Basic fires the
UserControl's
WriteProperties event just before it fires the
UserControl's
Terminate event provided that at least one property value has changed. In other words,
the
WriteProperties event fires whenever the current instance of the control
is about to be destroyed and any property values that you want to persist have changed and, therefore,
need to be saved.
As its name implies, you use the WriteProperties event procedure to save
persistent property values. The specific mechanism you use to save property values is to call the
WriteProperty method of the Property Bag for each property whose
value you wish to save. The Property Bag is available in the event
procedure of the WriteProperties event as a parameter named
PropBag. The example code in Listing 13.7 shows how you would call the
Property Bag's WriteProperty method to save individual
property values. Notice that we use whatever repository has been storing the property value
as the source for the current value: at times this might be a private memory variable, and
at other times it might be a property of a constituent control (as in the final line before the
End Sub).
LISTING 13.7
USING THE WRITEPROPERTIES EVENT PROCEDURE TO SAVE PROPERTY VALUES TO THE PROPERTY BAG
Private Sub UserControl_WriteProperties _
(PropBag As PropertyBag)
'Store the values of the custom properties
'to the Property Bag
PropBag.WriteProperty _
"BackColor", BackColor
PropBag.WriteProperty _
"Celsius", m_Celsius
PropBag.WriteProperty _
"Fahrenheit", m_Fahrenheit
PropBag.WriteProperty _
"TemperatureDate", m_TemperatureDate
PropBag.WriteProperty _
"Caption", lblCaption.Caption
End Sub