Visual Basic Concepts

Creating an ActiveX Control

The series of step-by-step procedures in this chapter builds a simple ActiveX control called ShapeLabel. Although the control itself is not very interesting, building it will quickly demonstrate the major events in the life of an ActiveX control, introduce you to the intricacies of running code at design time, and show the basic steps for creating and hooking up a property page.

All of the subjects introduced in these procedures are covered in greater depth in later chapters. References to in-depth material will be found in each procedure. In addition, "Building ActiveX Controls," shows how you can use the ActiveX Control Interface Wizard to make building controls even easier.

The procedures for creating the ShapeLabel control build on each other, so the sequence in which you perform the procedures is important.

Topics

Creating the ControlDemo Project

Opens a blank ActiveX control project, examines the ActiveX control designer, and sets the appropriate project properties.

Adding the TestCtlDemo Project

Creates a project group, including the ActiveX control and a test project, and links the two projects.

Running the ShapeLabel Control at Design Time

Shows how to put a single ActiveX control into run mode, without putting the test project into run mode, so the control’s design-time behavior can be debugged.

Life and Times of a UserControl Object

Illustrates the major events that occur in the life of an ActiveX control, and shows how an instance of a control is continually destroyed and re-created.

Drawing the ShapeLabel Control

You can draw your control from scratch, with graphics methods, or construct it by combining other controls. ShapeLabel uses the latter technique.

Saving the ShapeLabel Control's Property Values

Adds a Caption property to the ShapeLabel control, and demonstrates how to save the value. Introduces the Extender object.

Giving the ShapeLabel Control a Property Page

Shows how to create a property page, and how to connect it with the ShapeLabel control.

Adding an Event to the ShapeLabel Control

Demonstrates how to expose events.

Compiling the ControlDemo Component

Compiles the project, and shows how to test the .ocx file with the test project, and how to include it in another project.

Control Creation Recap

Summarizes the key tasks required to create an ActiveX control.

These procedures will be easier to follow if you set up your Visual Basic development environment to show the necessary windows.

Before You Begin

  1. On the View menu, click Toolbox to open the Toolbox.

  2. On the View menu, click Project Explorer to open the Project Explorer window. The Project Explorer window will be used extensively to switch between project files.

  3. If the Project Explorer window is in Folder view, as shown below, click the Toggle Folders button on the Project Explorer window toolbar to turn the folders off.

  4. On the View menu, click Properties window to open the Properties window.

  5. On the View menu, click Immediate window to open the Immediate window. You will need this window open at design time, in order to demonstrate the control’s code running at design time.

  6. On the Tools menu, click Options to open the Options dialog box.

    Select the Editor tab, and make sure the Require Variable Declaration check box is selected. This makes it much easier to catch typing errors.

    Select the Environment tab. Make sure Prompt To Save Changes is checked, then click OK. This will make it easy to save the changes to the project as you go along.

Sample application

CtlPlus.vbg

Fills in all the properties, methods, and events required to make ShapeLabel a functional control. Expands on the material in this chapter, showing additional control creation features, at the expense of some of the basic material covered in the step-by-step procedures. The sample applications are listed in the directory.