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
property's
Property procedures and modifying the lines
in the
InitProperties, ReadProperties,
and
WriteProperties event procedures that manage the
property's value.
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
and Property
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
txtEntry.
-
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.
LISTING 13.13
PROPERTY LET AND PROPERTY GET PROCEDURES FOR A DELEGATED PROPERTY
Property Let EntryText(strValue as String)
txtEntry.Text = strValue
PropertyChanged "EntryText"
End Property
Property Get EntryText() as String
EntryText = txtEntry.Text
End Property
LISTING 13.14
INITIALIZING A DELEGATED PROPERTY IN THE INITPROPERTIES
EVENT PROCEDURE
Private Sub InitProperties()
'Do some other initialization
activies....
txtEntry.Text = ""
End Sub
LISTING 13.15
MANAGING A PERSISTENT DELEGATED PROPERTY IN THE READPROPERTIES AND WRITEPROPERTIES EVENT
PROCEDURES
Private Sub WriteProperties(PropBag As PropertyBag)
'Write some other properties...
PropBag.WriteProperty _
"EntryText", txtEntry.Text
End Sub
Private Sub ReadProperties(PropBag As PropertyBag)
'Read some other properties...
txtEntry.Text = PropBag.ReadProperty _
("EntryText", "")
End Sub