Visual Basic Concepts
Properties You Should Provide
Recommended properties include Appearance, BackColor, BackStyle, BorderStyle, Enabled, Font, and ForeColor. It's also a good idea to implement properties commonly found on controls that provide functionality similar to yours.
In addition, you may wish to selectively implement properties of any constituent controls on your UserControl object, as discussed in "Exposing Properties of Constituent Controls," earlier in this chapter.
All of the above properties should use the appropriate data types or enumerations, as discussed in the related topics "Using Standard Control Property Types" and "Exposing Properties of Constituent Controls."
Note If you're authoring a control that provides its appearance using constituent controls, implementing the Appearance property is problematic. For most controls, the Appearance property is available only at design time — but you can only delegate to run-time properties of constituent controls.
Procedure IDs for Standard Properties
Every property or method in your type library has an identification number, called a procedure ID or DISPID. The property or method can be accessed either by name (late binding) or by DISPID (early binding).
Some properties and methods are important enough to have special DISPIDs, defined by the ActiveX specification. These standard procedure IDs are used by some programs and system functions to access standard properties of your control.
For example, there's a procedure ID for the method that displays an About Box for a control. Rather than rummaging through your type library for a method named AboutBox, Visual Basic calls this procedure ID. Your method can have any name at all, as long as it has the right procedure ID.
To assign a standard procedure ID to a property
On the Tools menu, click Procedure Attributes to open the Procedure Attributes dialog box.
In the Name box, select the property.
Click Advanced to expand the Procedure Attributes dialog box.
In the Procedure ID box, select the procedure ID you want to assign to the property. If the procedure ID you need is not in the list, enter the number in the Procedure ID box.
Important Selecting (None) in the procedure ID box does not mean that the property or method will not have a procedure ID. It only means that you have not selected a particular procedure ID. Visual Basic assigns procedure IDs automatically to members marked (None).
One Procedure ID to a Customer
A property or method of a control can have only one procedure ID, and no other property or method of the control can have the same procedure ID.
That is, every control in your control component can have a default property (procedure ID = 0), but only one property on each control can have that procedure ID.
If you assign the same procedure ID to two different members, the one that comes first in the type library is the only one that can be accessed. The other might as well not exist.
Procedure IDs of Interest
It's always a good idea to assign the standard procedure ID to a property, if there is one. The Procedure Attributes dialog box lists procedure IDs by the property name they are usually associated with. You may find the following IDs of particular interest.
AboutBox
Allows you to specify a method that shows an About Box for your control, as discussed in "Adding an About Box to Your Control," earlier in this chapter. There is no particular method name associated with this ID.
Caption, Text
Either of these procedure IDs will give a property the Properties window behavior demonstrated by the Caption and Text properties of Visual Basic intrinsic controls. That is, when a user types a value into the Properties window, the new value will be reflected immediately in the control.
This means that your Property Let procedure will be called for each keystroke the user enters, receiving a complete new value each time.
The property you assign these to need not be called Caption or Text, although those properties represent the kind of functionality these procedure IDs were designed to support.
(Default)
The default property of a control is the one that will be accessed when no property has been specified. For example, the following assigns the string "Struthiomimus" to the (default) Caption property of Label1:
Label1 = "Struthiomimus"
Enabled
This procedure ID must be assigned to the Enabled property of your control, in order for its enabled/disabled behavior to match that of other controls. This is discussed in "Allowing Your Control to be Enabled or Disabled," earlier in this chapter.
For More Information The procedure IDs of interest to control authors are listed in the Procedure ID box of the Procedure Attributes dialog box. For a complete list of DISPIDs, consult the ActiveX specification.
Providing Useful Defaults
Whenever you implement a property with the same name as one of the standard ambient properties, such as BackColor, Font, and ForeColor, you should consider whether the value of the corresponding property of the AmbientProperties object would be a useful default.
You can see an example of this behavior by changing the size of the font on a Visual Basic form, and then adding a label or command button. The new control uses the form's current font settings as its default font settings. Most of the intrinsic controls follow this example.
If you're authoring a check box, option button, or label, setting the control's default BackColor to match AmbientProperties.BackColor might be a useful service to your users.
Clearly, this requires some thought about how controls are used. For example, on a text box the Font property would be a good candidate for ambient matching, while the BackColor property would not.
For More Information See "Using the AmbientProperties Object to Stay Consistent with the Container," earlier in this chapter.
Using the ActiveX Control Interface Wizard
The ActiveX Control Interface Wizard can assist you in determining what properties to provide, and in delegating to the appropriate constituent controls.
After you have placed all the constituent controls you're going to use on your UserControl, start the wizard and select your control. The wizard will examine your constituent controls, and produce a list of all the properties, methods, and events that appear in all their interfaces, plus those in the UserControl object's interface, and the standard properties listed above. You can select from this list those properties, methods, and events you want in your control's interface.
The wizard will produce default mappings of your control's properties to properties of the UserControl object or of constituent controls. In subsequent steps, you can modify these mappings.
When you have finished determining your control's interface and delegating to existing properties, the wizard will generate property procedure code to implement the properties, using the correct data types for standard properties, and including delegation code for all your property mappings, enormously reducing the amount of work required to generate a full-featured control.