event fires just once in the lifetime
of a control: When the developer first sites it on its container from the toolbox.
The InitProperties event procedure is therefore the perfect
place to set initial default values for your control's properties since you'll
want these default values to show up when the developer first sites the control on its container.
However, you won't want the default values to override any later changes the developer
makes after siting the control.
You will mainly use InitProperties to initiate default values
for your custom control's properties. If you've implemented a property as a
Public variable, you can simply set the variable name to the desired default value.
On the other hand, if you're using Property procedures to implement a property,
you can set its default value by writing to the Private memory
variable or other holder (such as a constituent control property) that maintains the property's
You can set these default properties in a number of ways, depending on the type of
property you want to set and on how you've chosen to implement the property:
Assign a literal value to the Private storage variable implementing
the custom property.
Assign a default constant value (as discussed in the previous section) to the
Private storage variable.
Derive a property's initial value from some Extender property.
Derive a property's initial value from some Ambient property.
The InitProperties event procedure shown in Listing 13.6
gives examples of the techniques listed above.
The code in the following listing assumes that Slider and
TextBox controls exist in the project with respective names Slider1
and txtDate. It also pre-supposes Private
variables and constants that implement property values and their defaults, as shown
in the General Declarations section of the listing.
VARIOUS TECHNIQUES FOR INITIALIZING PROPERTIES TO DEFAULT VALUES IN THE INITPROPERTIES EVENT
Private Const defCelsius = 30
Private m_TemperatureDate As Date
Private m_Celsius As Single
Private m_Caption as String
Private Sub UserControl_InitProperties()
to a value
'determined right here
m_TemperatureDate = DateSerial(1997,
'Set Celsius to a default
m_Celsius = defCELSIUS
'Set the Background
'match the container's
BackColor = Ambient.BackColor
'Set the caption to
'same as the object's
'm_Caption = Extender.Name
'and set Slider to reflect
Slider1.Value = m_Celsius
'and textbox to match
txtDate.Text = m_TemperatureDate
Note in the listing that variable names storing property values do not follow
the usual Hungarian notation naming convention (a two-four letter lowercase prefix
indicating variable type). Instead, variable names that refer to the intermediate
storage of the values of properties implemented with Property
Get/Let/Set are prefixed with "m_"—a Microsoft documentation standard
for Class modules. Microsoft's Control Wizard generates such variable names, automatically
deriving them from the names of properties that you specify in the wizard.