If you wish to expose properties of your control's constituent controls to
the developer, then you will need to implement delegated properties. A delegated
property is a custom control property that provides a wrapper for the property
of an underlying constituent control usually by placing code in the delegated
procedures and modifying the lines
in the InitProperties, ReadProperties
event procedures that manage the
A delegated property is the only way you can give the developer access to the
properties of a constituent control since constituent controls are private to
the UserControl object and so their very existence,
let alone their individual members, are unknown to the developer.
You may give the delegated property the same name as the constituent control
property it implements, or you might give it a different name to distinguish it
from the actual constituent control's property.
As an example of a delegated property, let's say that you have a constituent
control, txtEntry, and you wish to expose its Text
property to the developer using your control. You could create a custom property
EntryText to delegate the txtEntry.Text property.
To accomplish the property delegation, you'd follow these steps:
STEP BY STEP
13.2 Property Delegation
Create Property Get
Let procedures for the
EntryText property (see Listing 13.13).
Note that we don't use a
Private variable in this example
to store the property's value: Rather,
we use the Text property
of the constituent control
Put a line of code in the InitProperties event to implement
the default value for this property. Note again in Listing 13.14 the use of the
Text property of the constituent control rather than a Private
variable as our repository for the property's value.
Put the appropriate code to manage the property's value into the
ReadProperties and WriteProperties event procedures. Note once again
in Listing 13.15 that we use the constituent control's Text
property (and not a variable) to store and retrieve the property's value.
PROPERTY LET AND PROPERTY GET PROCEDURES FOR A DELEGATED PROPERTY
Property Let EntryText(strValue as String)
txtEntry.Text = strValue
Property Get EntryText() as String
EntryText = txtEntry.Text
INITIALIZING A DELEGATED PROPERTY IN THE INITPROPERTIES
Private Sub InitProperties()
'Do some other initialization
txtEntry.Text = ""
MANAGING A PERSISTENT DELEGATED PROPERTY IN THE READPROPERTIES AND WRITEPROPERTIES EVENT
Private Sub WriteProperties(PropBag As PropertyBag)
'Write some other properties...
Private Sub ReadProperties(PropBag As PropertyBag)
'Read some other properties...
txtEntry.Text = PropBag.ReadProperty _