Programming Components with Microsoft Visual Basic 6.0
Creating
ActiveX Controls
Writing Your Own Control from Scratch
Writing a control from scratch affords you the greatest flexibility since you are not limited by any existing functionality. This allows you to do anything you want with your control’s appearance and interface simply by putting code into the Paint event procedure used to draw your control. This is the model to select when creating a new visual widget such as a super-duper button.
Enhancing an Existing Control
Enhancing an existing control means using that control as a starting point and then adding your own properties, methods, and events. You have complete freedom in specifying the programmatic interface for your enhanced control. The constituent control’s properties will be exposed only if you decide to expose them.
Modifying the appearance of an existing control is more difficult than creating a control from scratch, however, because the control you are enhancing already contains code to paint itself. Experienced Microsoft Windows programmers can use the AddressOf operator to make the constituent control a subclass so that it will notify you of its Paint events, but this is not an easy task. Using these hybrid controls is similar to what is done in the object-oriented concept of aggregation. Recall that aggregation occurs when you build a new object by accumulating objects that already exist.
Assembling a New Control
from Several Existing Controls
The last control creation model expounded by Visual Basic is assembling a control from several existing controls. This model is similar to the second model in that they both utilize existing controls to build new controls.
The UserControl Object
An ActiveX control created in Visual Basic is always composed in a UserControl object, just as a Visual Basic application is always composed in a Form object. Like a Visual Basic Form object, a UserControl object has a code module and a visual designer (the UserControl designer), as shown in Figure 6-1. Note that you can also use class modules inside an ActiveX control project. In addition to a UserControl object, an ActiveX control in Visual Basic can have any other controls—called constituent controls—that you choose to place on the UserControl object. You use constituent controls only if you want to enhance an existing control or create a hybrid control consisting of several existing controls. You place constituent controls on the UserControl designer and set their properties in the same way you do when placing controls on a form.
next page....
|