Documente Academic
Documente Profesional
Documente Cultură
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.]
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.
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.
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.
In this section, we will create our own Timer ActiveX control that can be used in Access
and other applications.
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.
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.
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:
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."
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,
The first screen of the wizard is the "Introduction" screen. Click on the "Next" button.
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.
On the "Create Custom Interface Members" screen, click on the "Next" button, since we
have already added the property and event we wanted.
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.
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.
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.
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.
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:
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.
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:
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.
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.
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:
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.
Now that we have tested our Timer control in Visual Basic, let's use it in an Access form.
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.
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.
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 "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.
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.
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.
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.
UserControl: "cSlider"
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.
The cSLider control now works on the Access form. As the UpDown control changes, the
slider changes as well.
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.
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.
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.
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.
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.
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.
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.
A "Deployment Report" is then created and can be saved for later use.
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.
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"
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.
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.