Freigeben über


Visual Basic Concepts

The UserControl Object

An ActiveX control created with Visual Basic is always composed of a UserControlobject, plus any controls — referred to as constituent controls — that you choose to place on the UserControl.

Like Visual Basic forms, UserControl objects have code modules and visual designers, as shown in Figure 9.1. You place constituent controls on the UserControl object's designer, just as you would place controls on a form.

Figure 9.1   UserControl designer and code window

Like forms, user controls are stored in plain text files that contain the source code and property values of the UserControl and its constituent controls. Visual Basic uses the extension .ctl for these source files.

The relationship of .ctl files and ActiveX control projects to finished controls and .ocx files is shown in Figure 9.2.

Figure 9.2   ActiveX control projects are built into .ocx files

If a UserControl or its constituent controls use graphical elements which cannot be stored as plain text, such as bitmaps, Visual Basic stores those elements in a .ctx file with the same name you give to the .ctl file. This is analogous to the .frx files used to store graphical elements used in forms.

The .ctl and .ctx files completely define an ActiveX control's appearance and interface (properties, methods, and events). You can include .ctl files in any of the project types. "Two Ways to Package ActiveX Controls," later in this chapter, discusses this subject in depth.

Delegating to the UserControl and Constituent Controls that Compose Your ActiveX Control

Your ActiveX control is said to be composed of a UserControl and its constituent controls because each instance will actually contain those objects.

That is, whenever you place an instance of your ActiveX control on a form, a UserControl object is created, along with instances of any constituent controls you placed on the UserControl designer. These objects are encapsulated inside your control.

The UserControl object has an interface — that is, properties, methods, and events — of its own. The interface of your ActiveX control can delegate to the UserControl object's interface members, which are hidden from the user of your control by encapsulation.

That is, rather than writing your own code to implement a BackColor property, you can delegate to the UserControl object's BackColor property, and let it do all the work. In practice, this means that the BackColor property of your ActiveX control simply calls the BackColor property of the UserControl object.

In the same manner, you can piggy-back your control's Click event on the existing functionality of the UserControl object's Click event.

The interface for your ActiveX control can also delegate to the properties, methods, and events of the constituent controls you place on the UserControl designer, as discussed in "Exposing Properties of Constituent Controls," "Adding Methods to Controls," and "Exposing Events of Constituent Controls," later in this chapter.

For More Information   For a discussion of what controls you can place on a UserControl designer, see "Controls You Can Use As Constituent Controls," later in this chapter.