Just as you do with other object classes in VB, you define custom properties
for ActiveX controls either with
Public variables or
with
Property procedures
(Property
Let/Set and Property Get).
Because it's very likely that a developer will use more than one instance
of your control in an application, the use of Public variables
is strongly discouraged. Values for the same property in different instances of
the control could become confused if a Public variable
were used to implement a property that the system accessed in more than one instance
of a control.
Instead, you will always want to use Property procedures
to implement control properties. As you'll recall from other discussions of class
properties, you'll need to set up the following elements for each custom property:
-
A variable that is Private to the UserControl and that will serve as a storage place for
the property's actual value. Property Get and
Property Let/Set will respectively retrieve and store this value.
A Property Let or Property Set procedure that acts
like a Sub procedure and accepts a parameter whose value is the new value to be assigned
to the property. You will store this parameter's value in the Private storage variable just
mentioned in the previous point.
A Property Get procedure that acts like a Function procedure and
returns the value kept in the Private storage variable.
The code in Listing 13.5 illustrates how you would implement your own property for
a custom control. Notice that we use Property procedures and the Private
variable in almost exactly the same way as we do for custom properties in standard Class
modules.
The only difference in Listing 13.5 from standard class modules'
Property procedures is the extra line in the Property
Let procedure that calls the PropertyChanged method.
This call notifies VB that a property has been changed and ensures that the
WriteProperties event will fire at the appropriate moment. For more detail,
see the following section, "Calling the
PropertyChanged Method to Trigger WriteProperties".
You must take into account a number of extra considerations to make your custom
control properties work properly. In the following sections, we discuss what you need to know in
order to create fully functioning custom properties for your controls.
LISTING 13.5
IMPLEMENTING A PROPERTY FOR A CUSTOM CONTROL
[in the general declarations section of the UserControl]
Option Explicit
Private mOverallColor As Long
Property Get OverallColor() As Long
OverallColor = mOverallColor
End Property
Property Let OverallColor(lColor As Long)
mOverallColor = lColor
'informs the system
that a
'property has changed:
PropertyChanged
End Property
NOTE - Property Procedures : See the subsection of
Chapter 12, "Creating a COM Component that Implements
Business Rules or Logic" under the main section "Compiling
a Project with Class Modules into a COM Component," to review how to use Property procedures including
the concept of how to implement a readonly property by not supplying a Property Let procedure
NOTE - Storing Property Values in Delegated Properties : Instead
of storing the custom property's value in a Private variable,
you might choose to store its value in a property of one of the UserControl
object's constituent controls. You would then be implementing a delegated property,
as discussed in "Implementing Delegated Properties."
NOTE - Automatically Implementing Color Dialog Boxes for
Properties of Type OLE_COLOR:
If you create a custom property and declare its type to be
OLE_COLOR, then VB will automatically show the Color
property dialog box to the developer in the Properties Page of an instance
of your control. Remember that you must then consistently refer to its type as
OLE_COLOR throughout the control's code (in its Private
variable and Property procedure declarations).