Sie sind auf Seite 1von 43

Building Your Own

ActiveX Controls
with Visual Basic
By Tom Howe
[Portions of this material are from "Access 2000 Development Unleashed" (SAMS).
Authors: Forte, Howe & Ralston. Copyright 1999. All Rights Reserved.]

Building Your Own ActiveX Controls with Visual Basic 1


Copyright © 2002 by Tom Howe
All rights reserved
Creating ActiveX Controls
Visual Basic is exciting, because it allows you to create your own ActiveX controls, giving
you enormous power to extend your applications.

Tip: The ActiveX components and ActiveX controls discussed in this paper are included
in the sample code. These components must be registered before you can use them.
To register the components, open the project file and compile it. Additional
information about registering components is discussed in later in this paper.

Types of ActiveX Controls


ActiveX Controls are of two types: run-time controls and design-time controls. Examples
of run-time controls are the TreeView and ListView controls, had an interface for user
interaction. Design- time controls, on the other hand, are used by developers only; users do
not interact with these controls. Examples include the ImageList and CommonDialog
controls.

ActiveX Control Basics


ActiveX controls cannot exist independently. They must be instantiated in a host
application, typically a form.

A compiled ActiveX control is saved in a file, typically with an OCX extension. A single
OCX file may contain several ActiveX controls. For example, MSCOMCTL.OCX shipped
by Microsoft contains the following ActiveX controls: ImageCombo, ImageList, ListView,
ProgressBar, Slider, StatusBar, TabStrip, ToolBar and TreeView.

ActiveX controls are in-process Automation servers. Therefore, an ActiveX control you
create and use on an Access or Visual Basic form runs in the same process as the Access or
Visual Basic application, which results in excellent performance.

2 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Creating a Design-Time ActiveX Control
In Access, when a timer is needed, developers generally use the "On Timer" property and
"Timer" event of a form. One problem with this approach is only one timer can be used.
What if you need multiple timers, each working independently.

Visual Basic includes a built- in Timer control not available in Access. The VB Timer
control is a design- time control which can execute code at certain intervals. To use the VB
Timer control, place it on the form and set the "Interval" property in milliseconds (1000
milliseconds equal one second). Code in the "Timer" event executes whenever the interval
occurs. Multiple VB timers can be placed on a form, each operating independently.

Set the "Interval" Property of the Timer Control

In this section, we will create our own Timer ActiveX control that can be used in Access
and other applications.

Building Your Own ActiveX Controls with Visual Basic 3


Copyright © 2002 by Tom Howe
All rights reserved.
Tip: The VB ActiveX Control project and Access database that uses the Timer control
are both available in the sample code. Remember, the ActiveX control must be
registered (compiled) on your computer in order to use it.

Starting an ActiveX Control Project


To create your ActiveX control, open Visual Basic. In the "New Project" dialog box,
choose "ActiveX Control."

Choose ActiveX Control from the "New Project" Dialog Box

Creating the Interface


When the project opens, you will see what appears to be a form. Actually, it is not a form,
but is called a "UserControl" object. The "UserControl" is used to create the interface for

4 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
the ActiveX Control. In the properties window for the "UserControl," change the name
from "UserControl1" to "ctlTimer."

ActiveX Controls may include other controls. In our Timer control, we will insert a
"PictureBox" control from the toolbox. Name the PictureBox control "picClock" and
choose for its "Picture" property the "CirClock.bmp" from the "Bitmaps\Guage" directory
where you installed Visual Basic. This picture of a clock is what the developer will see
when using the design-time control.

Insert a PictureBox Control on the UserControl

Next, insert a VB Timer control from the Toolbox, and name it "ctlVBTimer." Notice in
the properties window of the control that there is an "Interval" property. We will be using
this property in our ActiveX control.

Building Your Own ActiveX Controls with Visual Basic 5


Copyright © 2002 by Tom Howe
All rights reserved.
Insert a VB Timer Control on the UserControl

Now, we will resize the UserControl object. First, move the VB Timer control and place it
directly on top of the PictureBox control. Then choose "S end to Back" under the "Format"
menu. This will hide the control. Next, resize the UserControl so it fits around the
PictureBox control as shown in the figure that follows:

6 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Resize the UserControl

Since this is a design-time control, the end-user will never see it. In the UserControl
properties window, set the "InvisibleAtRuntime" property to "True."

Setting Project Properties


Open the "Project1 Properties" under the "Project" menu. In the "Project Properties" dialog
box, enter the "Project Name" (cTimerControl) and "Project Description" (cTimer
Control). Click "OK" to close the "Project Properties" dialog box.

Building Your Own ActiveX Controls with Visual Basic 7


Copyright © 2002 by Tom Howe
All rights reserved.
Specify Project Name and Description in Project Properties

Saving the Project


Save the project and UserControl by choosing "Save Project" under the "File" menu. It is
best to save the project (.vbp file) and UserControl (.ctl file) in the same directory (e.g.,
"Timer Control").

Adding a Method and Event


Click on the UserControl (not the PictureBox) and review the list of properties in the
Properties window. Notice there is no "Interval" property. You will need to add this
property. Also, a "Timer" event must be added to enter code to execute when the Timer
interval occurs. To add properties, methods and events to an ActiveX control, use the
"ActiveX Control Interface Wizard."

Using the ActiveX Control Interface Wizard

Open the ActiveX Control Interface Wizard by choosing "ActiveX Control Interface
Wizard" under the "Add-Ins" menu. If you do not see the item under the "Add-Ins" menu,

8 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
choose "Add-In Manager,” select the "VB 6 ActiveX Control Interface Wizard," click on
the "Load on Startup" checkbox, and then click the "OK" button to close the dialog box.

The first screen of the wizard is the "Introduction" screen. Click on the "Next" button.

ActiveX Control Interface Wizard: "Introduction" Screen

On the right side of the "Selected Members Interface" screen is a list of standard
properties, methods and events. Remove all of the items on the "Right" side. On the left
side, choose the "Interval" property and "Timer" Event, and move them to the right side so
they are included in the control. Click on the "Next" button.

Building Your Own ActiveX Controls with Visual Basic 9


Copyright © 2002 by Tom Howe
All rights reserved.
"Selected Members Interface" Screen

On the "Create Custom Interface Members" screen, click on the "Next" button, since we
have already added the property and event we wanted.

10 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
"Create Custom Interface Members" Screen

On the "Set Mapping" screen, select the "Interval Property" and the "Timer" event, and
map them to the "UserControl." Then click on the "Next" button.

Building Your Own ActiveX Controls with Visual Basic 11


Copyright © 2002 by Tom Howe
All rights reserved.
"Set Mapping" Screen

On the "Set Attributes" screen, choose the "Interval" property and make sure the data type
is set to "Long." Then click on the "Next" button.

12 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
"Set Attributes" Screen

On the "Finished" screen, click on the "Finished" button. The wizard will write code for
you to add the "Interval" property and the "Timer" event to the ActiveX control. Also, a
summary report is created that can be saved to disk.

Building Your Own ActiveX Controls with Visual Basic 13


Copyright © 2002 by Tom Howe
All rights reserved.
"Finished" Screen

Open the code window for the UserControl and examine the code. The wizard created a
Propery Get and Property Let for the "Interval" property. Also, code was written to add the
"Timer" event.

14 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Code Created by the ActiveX Control Interface Wizard

Adding Code to the ActiveX Control


The ActiveX Control Interface Wizard did a great deal of work for us, but we need to add a
little code to complete our Timer control.

Raising an Event

In our ActiveX control, there is a VB Timer control with a "Timer" event. Our ActiveX
control also has a "Timer" event that was added with the ActiveX Control Interface
Wizard. We need to synchronize these Timer events so that when the VB Timer event
fires, it triggers our ActiveX controls Timer event.

To do this, we will "Raise" an event in our UserControl. In the code window, choose
"ctlVBTimer" from the object list on the top left-hand drop-down list. Then choose
"Timer" from the event list on the top right- hand drop-down list. A Timer event procedure
will then be created. Enter the following code to "Raise" the Timer Event:

Building Your Own ActiveX Controls with Visual Basic 15


Copyright © 2002 by Tom Howe
All rights reserved.
RaiseEvent Timer

So what is the effect of this? When the VB Timer executes, its "Timer" event fires, which
then triggers the "Timer" event of the ActiveX control. This will run the code you create
for the Timer control.

Using ReadProperties Event

When a developer inserts our Timer control on a form, the "Interval" property will be set to
determine when the "Timer" event fires. The VB Timer control will take care of the time
keeping. Therefore, the value in the "Interval" property of our Timer control must be
synchronized with the "Interval" property of the VB Timer control.

ActiveX controls are created and destroyed often, as they are added to forms by developers
and when forms that contain the control are opened. To synchronize the "Interval"
properties, the "ReadProperties" event is a good choice, since it will execute when the form
hosting the control opens. The following code will set the value of the "Interval" property
in the VB Timer control to be the same as our Timer control:

16 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
' When the interval on our control is changed on the form.
' change the interval on the VBTimer control in our control.
UserControl.ctlVBTimer.Interval = Me.Interval

Make sure to save the project.

Testing the ActiveX Control in Visual Basic


Before using our Timer control in Access and other applications, let's test it in Visual
Basic.

With the ActiveX Control project open, we can add a "Standard EXE" project and test our
control on a VB form. To add a project, choose "Add Project" under the "File" Menu. In
the "New Project" dialog box, select "Standard EXE." Notice that there are now two
projects displayed in the "Project Explorer" window.

Add a Project to Test the ActiveX Control

Building Your Own ActiveX Controls with Visual Basic 17


Copyright © 2002 by Tom Howe
All rights reserved.
Because there are now two projects, set the "Startup Project" to the Standard EXE
(Project1). In the "Project Explorer" window, right- mouse click on "Project1." On the
menu, choose "Set as Startup." Now when you run the project, the Project1 will start.

Next, close all of the ActiveX Control project windows. Under the "Windows" menu,
select any open windows (except Form1) and close each window.

While in the design view of Form1, open the Toolbox. Notice that on the Toolbox is a new
control named ctlTimer. Insert the control on the VB form.

Insert the Timer Control on a VB Test Form

Our Timer control on the form can be used just like any other control (e.g., textbox). Its
properties can be set in the properties window, and code can be written to respond to
events.

In the properties window, set the "Interval" property to 5000. Since the control uses
milliseconds, the "Timer" event will execute every five seconds.
18 Building Your Own ActiveX Controls with Visual Basic
Copyright © 2002 by Tom Howe
All rights reserved.
Next, choose "Code" under the "View" menu to open the code window. Choose
"ctlTimer1" from the object list and "Timer" from the event list. In the Timer event
procedure, enter a simple message box as follows:

Timer Event Procedure

Run the project by choosing "Start" under the "Run" menu (or press "F5"). Every five
seconds the message box will appear. Notice that the control cannot be seen at runtime.
Any type code can now be written utilizing our Timer control.

Building Your Own ActiveX Controls with Visual Basic 19


Copyright © 2002 by Tom Howe
All rights reserved.
Message Box Opens Every Five Seconds

Now that we have tested our Timer control in Visual Basic, let's use it in an Access form.

Using Timer Control in an Access Form


Testing our control was very easy in Visual Basic. To use the control in Access, we need to
compile the control.

Open the ActiveX Control project, and choose "Make cTimerControl.ocx" from the "File"
menu. Save the OCX file in the same directory as your ActiveX Control project.

The ActiveX control is now compiled, and registry settings have been made on your
computer.

Open a form in design view in an Access database. Choose "ActiveX Control" from the
"Insert" menu. Choose our Timer control ("cTimerControl") from the list, then choose
"OK" to close the dialog box.

20 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Choose the cTimerControl from the "Insert ActiveX Control"
Dialog Box

As we did in the VB test form, set the "Interval" property of the control to 5000, and create
a message box in the "Timer" event.

The cTimerControl will now work on an Access form.

Using Multiple Timer Controls


The beginning of this section mentions that the "OnTimer" event of Access forms is often
used as a timer. However, that is only one timer that can be used. We can place several
Timer controls on a form, and they will all work independently.

Building Your Own ActiveX Controls with Visual Basic 21


Copyright © 2002 by Tom Howe
All rights reserved.
Multiple Timer Controls Working Independently

Distributing the Timer ActiveX Control


Distributing an ActiveX control is essentially the same as distributing ActiveX
Components. See the white paper for the session "Creating Reusable Components."

Creating a Property Page


In Visual Basic, it is easy to create a property page for our ActiveX Control.

22 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
ImageList Control Property Page

We can create a Property Page for our Timer control as well! In the ActiveX Control
project, choose "Add Property Page" under the "Project" menu. In the "Add Property Page"
dialog box, choose the "VB Property Page Wizard." Click on the "Open" button to start the
wizard.

On the "Introduction" screen, click on the "Next" button.

On the "Select the Property Pages" screen, click on the "Add" button and add a "General"
page. Use the up arrow on the right-hand side of the screen to list the "General" page first.
Click on the "Next" button.

Building Your Own ActiveX Controls with Visual Basic 23


Copyright © 2002 by Tom Howe
All rights reserved.
VB Property Page Wizard: "Select the Property Pages" Screen

On the "Add Properties" screen, click on the "General" tab. Move the "Interval" property
from the left-hand list box to the right- hand list box. Click on the "Next" button.

24 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
VB Property Page Wizard: "Add Properties" Screen

On the "Finished" screen, click on the "Finished" button. The wizard will create the
Property Page and a summary report that can be saved to disk. Notice that the Property
Page now exists in the "Project Explorer" window.

Property Page in Project Explorer Window

Building Your Own ActiveX Controls with Visual Basic 25


Copyright © 2002 by Tom Howe
All rights reserved.
Using the Property Page
To use the Property Page, save and recompile the ActiveX control. In design view of an
Access form, insert the Timer cont rol, and right- mouse click on the control. On the menu,
choose "cTimerControl" properties. The property page can be used to enter the "Interval"
property.

Using the Property Page

Using the Timer Control in Other Applications


Our Timer control is not restricted to just VB and Access applications. This same control
can be used on a form in Word, Excel, Outlook and other applications.

26 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Creating a Run-Time ActiveX Control
Most ActiveX controls have an interface with which users interact. In this section, we will
create a run-time ActiveX control. The example control will be named cSlider and will
include a Slider control and UpDown control. As users increment and decrement the
UpDown control, the Slider control will move."

Tip: The VB ActiveX Control project and Access database that uses the cSlider control
are both available in the sample code. Remember, the ActiveX control must be
registered (compiled) on your computer in order to use it.

Creating the Interface


As with the design-time control, open an ActiveX Control project. On the UserControl,
add a Slider control and UpDown control as shown in the figure that follows. Notice that
ActiveX controls you build can include many other controls.

Building Your Own ActiveX Controls with Visual Basic 27


Copyright © 2002 by Tom Howe
All rights reserved.
Add a Slider and UpDown Control to the UserControl

Name the objects as follows:

UserControl: "cSlider"

Slider control: "ctlSlider"

UpDown control: "ctlUpDown"

Then, resize the UserControl.

Right-mouse click on ctlUpDown and choose "Properties" from the menu to open the
property page. On the property page, we will associate the UpDown control with the Slider
control using the "Buddy" property. The controls will then work together without writing
any code.

Click on the "Buddy" tab on the property page. In the "Buddy Control" text field, enter
"ctlSlider." This will associate the ctlSlider control with the ctlUpDown control. Choose
"Value" from the "Buddy Property" list so that when the value of ctlUpDown changes, the
value of ctlSlider will change. Enable the checkbox for "Auto Buddy," and choose "OK" to
close the property page.

28 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Set the "Buddy" Properties in the Property Page

Setting Project Properties


Open the "Project1 Properties" under the "Project" menu. In the "Project Properties" dialog
box, enter the "Project Name" (cSliderControl) and "Project Description" (cSlider
Control). Click "OK" to close the "Project Properties" dialog box.

Building Your Own ActiveX Controls with Visual Basic 29


Copyright © 2002 by Tom Howe
All rights reserved.
Specify Project Name and Description in Project Properties

Saving the Project


Save the project and UserControl by choosing "Save Project" under the "File" menu. It is
best to save the project (.vbp file) and UserControl (.ctl file) in the same folder (e.g.,
"cSlider Control").

Testing the Control on a Visual Basic Form


Test the cSlider control on a Visual Basic form following the same steps used to test the
design-time control discussed earlier.

Using the cSlider Control in an Access Form


Open the ActiveX Control project, and choose "Make cSliderControl.ocx" from the "File"
menu. Save the OCX file in the same directory as your ActiveX Control project. The
ActiveX control is now compiled, and registry settings have been made on your computer.

30 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Open a form in design view in an Access database. Choose "ActiveX Control" from the
"Insert" menu. Then choose "cSliderControl" from the list, and choose "OK" to close the
dialog box.

The cSLider control now works on the Access form. As the UpDown control changes, the
slider changes as well.

ActiveX Control on an Access Form

Distributing the cSlider ActiveX Control


Distributing an ActiveX control is essentially the same as distributing ActiveX
Components. See the white paper for the session "Creating Reusable Components."

Using the cSlider Control in Other Applications


Our cSlider control is not restricted to just VB and Access applications. This same control
can be used on a form in Word, Excel, Outlook and other applications.

Building Your Own ActiveX Controls with Visual Basic 31


Copyright © 2002 by Tom Howe
All rights reserved.
Creating an Internet Package (Setup)
Use the "Internet Package" in the "Package and Deployment Wizard" to use the cSLider
ActiveX controls on web pages.

Start the "Package and Deployment Wizard," and choose the "cSlider" project file (.vbp
file). Click on the "Package" button to go to the next screen. Choose "Internet Package,"
and click on the "Next" button.

Choose "Internet Package"

On the next screen, specify the package you want to deploy.

32 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Specify Package to Deploy

On the next screen, specify the "Deployment Method." You can deploy to a local or
network folder or to a Web server. In this example, a folder will be used.

Building Your Own ActiveX Controls with Visual Basic 33


Copyright © 2002 by Tom Howe
All rights reserved.
Specify Deployment Method

Specify a folder for the package files, and click on the "Next" button. The default creates a
"Package" folder as a subfolder in your project directory.

34 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Specify Package Folder

The "Included Files" screen will show all files needed to use the ActiveX control. This
includes the VB runtime file and any other necessary files. Click on the "Next" button.

Building Your Own ActiveX Controls with Visual Basic 35


Copyright © 2002 by Tom Howe
All rights reserved.
Files to Include in the Internet Package

On the "File Source" screen, specify the location of the files to install. The control you
create (OCX file) will generally be included in the cab (compressed) file. The default
setting for the VB runtime files and other Microsoft files is the Microsoft Web Site. You
may want to leave this default setting as is, since users will then be able to download these
files as needed from Microsoft's Web Servers. Click on the "Next" button to continue.

Tip: Specify the Microsoft Website for the location of the VB runtime and other
Microsoft files. Microsoft offers more bandwidth than most companies, which
ensures that the most updated files are installed.

36 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Specify the "File Source"

On the "Safety Setting" screen, the "cSlider" control is listed. Choose "Yes" for "Safe for
Scripting" and "Safe for Initialization." This is your declaration that the control is "safe."
Click on the "Next" button to continue.

Building Your Own ActiveX Controls with Visual Basic 37


Copyright © 2002 by Tom Howe
All rights reserved.
Specify "Safety Settings"

On the last screen, a script name can be specified to save a deploy script so that all of the
options need not be selected the next time the Deploy wizard is run. Click on the "Finish"
button to deploy the package.

38 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Specify a Script Name and Deploy the Package

A "Deployment Report" is then created and can be saved for later use.

Using the cSlider Control on Web Pages


Open the folder where the Internet Package was saved. Notice that a
"cSliderControl.HTM" file was created.

Building Your Own ActiveX Controls with Visual Basic 39


Copyright © 2002 by Tom Howe
All rights reserved.
Files in Internet Package Folder

Double-click on the "cSliderControl.HTM" file, and it will open in the browser. The
cSlider control works just as it did in VB and Access forms.

40 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
cSlider Control on a Web Page

Warning: Not all browsers support ActiveX controls. Microsoft Internet Explorer,
however, does support ActiveX controls and is shown in the preceding
figure.

To view the HTML for a web page, open Microsoft Internet Explorer and choose "Source"
under the "View" menu. Notice in the HTML that the cSlider ActiveX control and its Class
ID are shown as follows:

<OBJECT CLASSID="clsid:5220cb21-c88d-11cf-b347-00aa00a28331">
<PARAM NAME="LPKPath" VALUE="LPKfilename.LPK">
</OBJECT>
-->

<OBJECT ID="cSlider"
CLASSID="CLSID:A3251E4A-CCCF-11D2-A8AC-0010A4F61FE6"

Building Your Own ActiveX Controls with Visual Basic 41


Copyright © 2002 by Tom Howe
All rights reserved.
CODEBASE="cSliderControl.CAB#version=1,0,0,0">
</OBJECT>

ActiveX Control Object Code in HTML

Tip: If you want to use the cSlider ActiveX control on another web page, copy the
HTML object code from one web page to another.

42 Building Your Own ActiveX Controls with Visual Basic


Copyright © 2002 by Tom Howe
All rights reserved.
Summary
Using Visual Basic to create ActiveX code components and ActiveX controls can enhance
and extend applications. With ActiveX code components, the same code can be used in
many VB and other applications. This makes maintaining the code much easier, since
changes to the code only need to be made in one placethe code component.

Visual Basic allows you to create ActiveX controls to use in VB, Access, other
applications and even web pages. You can create run-time controls with an interface users
interact with, as well as design-time controls exclusively for developer use.

Building Your Own ActiveX Controls with Visual Basic 43


Copyright © 2002 by Tom Howe
All rights reserved.

Das könnte Ihnen auch gefallen