The system automatically fires the
WriteProperties and
ReadProperties events whenever it thinks you may need their services. To ensure that
the system knows a property has changed, you have to call the
PropertyChanged
method. An example of this would be when you change the value of a
Private variable that implements the value of a property. The system will have no
way of knowing that this variable is connected with a property, and therefore it will not fire
the
WriteProperties event based solely on the change you have made.
In such cases, you can call the PropertyChanged method. This method informs
the system that a particular property has changed and so ensures that the
WriteProperties event will fire before the current instance of the control is destroyed.
If you've written the appropriate code in the WriteProperties xvent,
then your property values will be stored in the Property Bag.
You should call the UserControl's
PropertyChanged method whenever you do something in code that will cause a change to
a property whose value you wish to persist. The most typical place for you to call the
PropertyChanged method would be in a Property Let or Property
Set procedure (see Listing 13.8). Note that we check the CanPropertyChange
method that we discuss in "Calling the CanPropertyChange Method Before Allowing
a Property Value to Change."
LISTING 13.8
CALLING THE PropertyChanged METHOD TO ENSURE THAT WRITEPROPERTIES WILL FIRE
Property Let Celsius(sValue As Single)
If CanPropertyChange("Celsius") Then
'assign incoming value to be stored
'in Private variable
m_Celsius = sValue
'invoke UserControl's PropertyChanged method
'so it knows to trigger WriteProperties and
'store new value
PropertyChanged ("Celsius")
'perform other housekeeping specific to this
application
Slider1.Value = m_Celsius
RecalcFahrenheitFromCelsius sValue
DisplayTempsFromSlider
End If
End Property