Creating your own ActiveX Controls
How
to make a very basic ActiveX Control, test it then compile
and use it |
|
This
is a very basic VB ActiveX Control project - An ActiveX
Control button is created which can be placed in a HTML
web page. When the button is clicked its caption changes.
Although its very simple this example includes all the
steps necessary to implement much more complex controls.
Start
out by launching Visual Basic |
| |
 |
|
| Select
the New tab |
|
|
| From the new project options, select ActiveX Control.
Either double-click the icon or select it and click on OK |
A
new ActiveX Control project will be created - For simplicity
we'll stick with the default project name, Project1 |
|
| What
you see looks very much like the familiar VB form you get when
creating the usual VB Executable project, except this has no
title bar: This is the VB ActiveX Control. It can be sized just
like a form and other controls from the VB Toolbox can be placed
on it. |
In
fact placinga control on it is what we will do next.
Grab the Command Button control from the toolbox and place it
onto the ActiveX control |
 |
 |
When
you place the command button on the ActiveX Control its property
box is displayed in VB, showing its default name Command1.
Again, for the purposes of this simple demonstration, we will
keep the default.
Select the control's Caption property and change its value to Click
me. The new value is shown both on the control
and in the properties, as
shown below. |
 |
| Next move the command button to the ActiveX Control's top left |
|
 |
| Now
resize the ActiveX Control so that it is the same size as the
command button - This ensures the control will look just like
a button when it's placed in a web page. |
Whatever
the control looks like here in the VB development environment
is what the user will see when viewing a web page containing
your ACtiveX Control. |
 |
| The
final programming step is to add some code to make the control
do something when the user responds to the inviting offer: Click
me |
You
can double-click the command button in VB to open the code window.
Add code to make the button's caption change, as shown below. |
 |
| If
you now run the project (F5, Start icon
or Run>Start)
you'll be prompted for information required to launch the project. |
Again,
to keep things simple, we'll leave the properties as they are
with our default name, UserControl1, as the
Start component |
 |
| Click
on OK to see the results. |
|
| |
Try
it! The page contains the simple ActiveX control described
on this page (If you can't/don't want to see the control in action,
this is a screen shot of the result in IE6) |
| The
project |
|
| |
|
| The
Try it! example uses a compiled version of the project, of course.
To put the control into a web page you need to include the compiled
.OCX file in the web page. |
|
To
compile the ActiveX Control click on File>Make Project1.ocx
When
you make the .ocx file VB also registers the ActiveX Control
which means it will have an entry in your registry. |
To
include the control in a page you will need to use the <OBJECT> tag, as follows:
<OBJECT
classid="clsid:6CDB7371-FCF2-41FC-9245-9CA782510D91"
codebase="Project1.ocx">
</OBJECT>
The
only contentious thing about this is, where does the 6CDB7371-FCF2-41FC-9245-9CA782510D91 come
from? And the answer is, from the registry entry.
There
are tools available that will embed ActiveX controls into HTML
but doing it manually is quite straightforward.
Run
regedit from the
command line and search HKEY_CLASSES_ROOT (use Edit>Find) for the
ActiveX Control project's name: In this instance, search for Project1.
Keep searching until the right-hand pane shows a (Default) for
Project1.UserControl1 with an AppID on the following line. What
follows the AppID is the class id for the control. Copy that to
the web page.
(OK,
it's a bit fiddly but it doesn't take that long!) |
| |
|