ActiveX Controls: Those are the controls you can add to the Visual Basic
toolbox using the Components dialog box. You can add those controls to a Visual Basic program like any other control. You can also use
ActiveX controls on the Internet, embedding them in your Web pages, as we’ll
see when we work on creating ActiveX controls. ActiveX controls can support
properties, methods, and events. Your
ActiveX control can be built entirely from scratch, it can be built on another
control (such as a list box), or it can contain multiple existing controls
(these ActiveX controls are said to contain constituent controls).
When you create an ActiveX control, you create
a control class file with the extension .ctl. Visual Basic uses that file to
create the actual control, which has the extension .ocx. After you register
that control with Windows the control will appear in the Visual Basic
Components dialog box, ready for you to add to a program. You can also use
these controls in Web pages.
Creating an ActiveX Control
1. Select the New Project menu item in the Visual Basic File menu to
open the New Project dialog box,
2. Select the ActiveX Control item in the New Project dialog box and
click on OK.
3. To design the calculator, add these controls in a vertical line in
the ActiveX control: a text box, Text1; a label with the caption “+”;
another text box, Text2; a command button, Command1, with the
caption “=”; and a third text box, Text3.
4. Now double-click the command button to open its Click event
handler:
Private Sub Command1_Click()
Text3.Text = Str(Val(Text1.Text) + Val(Text2.Text))
End Sub
And that’s it—we’ve created a new calculator ActiveX control.
Testing an ActiveX Control (In Browser): You can test, and even debug, ActiveX controls in the Microsoft
Internet Explorer. Just select the Start item in the Visual Basic Run menu to
see the ActiveX control you’re designing at work.
Registering an ActiveX
Control:
First, use the File menu’s Make activex.ocx
menu item to create activex.ocx. Next, we’ll use regsvr32.exe, which is usually
found in the C:\windows\system directory, to register that control with
Windows. Here’s how to register our ActiveX control:
c:\windows\system>regsvr32
c:\vbbb\activex.ocx
After the ActiveX control is registered, it will appear in the Visual
Basic Components dialog box
Creating A Visual Basic Project Group To Test An ActiveX Control:
1. Create a new Visual Basic standard EXE
project, Project1.
2. Select the Add Project item in the File menu.
3. Click the Existing tab in the Add Project
dialog box, select the name of the ActiveX calculator project and click on OK.
4. This adds the calculator ActiveX project to
the current project and creates a program group. Select the Select Project
Group As item in the File menu, accepting all default file names including the
group file itself, group1.vbg.
5. Close the calculator project’s window that
makes the calculator ActiveX control available to us in the other project and
it will appear in the toolbox.
6. Add a new calculator control to Form1 of
Project1.
7. Select the Run menu’s start item to start
Project1.