Building a Hello World ActiveX Control in VB
This set of instructions will show you how to build, test and package a
trivial ActiveX control. The control has no code attached to it: it simply
displays some text saying 'Hello World'.
Step 1. Building an ActiveX Control Project
- Start VB
- From the File Menu, select New Project
- Select ActiveX Control from the dialog box.
Step 2. Naming your Control Project
- From the Project Menu, select Project1 Properties
- In the dialog box, change the Project Name and Project description as
shown below. This will determine how your 'friendly' name of your component
is displayed later on, as well as the name of your compiled .ocx.
Step 3. Naming your actual control
- In the project explorer window, select UserControl1. Then change the
Name Property for user control 1 to HelloWord1. Your window should
look like the one below.
-
Original |
Renamed |
|
|
Step 4. Building your control
- Drop a label into the Control
- Using the Label properties, change its caption to "Hello World"
and its font to 14 point
- Create a 16x15 bitmap (using Microsoft paint) that has some kind of
descriptive image (you can just compose it by writing the letters HW). From
the HelloWorld UserControl properties, point the ToolboxBitmap to that
bitmap. It will eventually appear in the Toolbox on the left (rather than
the generic toolbox control image, shown greyed out on the bottom of the
left pane below).
Step 5. Saving your control
- This is probably a good time to save the project, just in case! Save it in
a folder called (say) HelloWorldV1
Step 6. Testing your control by two methods
- Now we will test this simple control via two methods.
- The first method uses Internet Explorer. If you click the play button, VB
will start Internet Explorer, and you will see your control in it.
- The second method for testing a control uses VB directly. From the File
menu, select Add Project and then select the Standard EXE icon (the dialog
box is the same as the one already seen in Step 1)
- From the Project explorer, right-click on Project1(Project1) to raise the
context menu, and select 'Set as Startup'. From now on, when you press the
play button it will start this program instead of using Internet explorer.
- Close the form containing your hello world control. and then raise the
Project1 Form.
- You will see an icon in the bottom of the toolbar: its tooltip will say
HelloWorld1. Select this, and drop it into the form. Your window should look
something like the one below.
- Feel free to browse the properties of your HelloWorld control in the
Properties window: you will see that its properties is not a label! Rather,
these are the default properties that every ActiveX control made in VB have.
- Just for fun, add a few more HelloWorld controls into your form window.
The nice thing about controls is that you can have lots of them!
Step 7. Testing the project
- Test the program by hitting play. It should bring up the following
window.
Step 8. Saving the project and the project group
- Save the project group (this is both the control and the sample
application you just made). To do this cleanly, when you save create a new
folder called "Test" under "HelloWorldV1" and save
everything to that.
Step 9. Compiling your OCX
- Through the project explorer, close Project1 and open up your
HelloWorld control again.
- Compile your control by selecting 'Make HCIHelloWorld1.ocx' from the file
menu. Save it in your HelloWorldV1 folder.
- Close down VB
Step 10. Testing your OCX from a new VB project.
- Start a new version of VB, and create a new .exe project.
- Select Components from the Project menu, as shown below. Scroll the list
until you find HCI Hello World Version 1. Check the checkbox, and then Ok.
This will add the hello world control icon to your controls window.
- Repeat Steps 7 and 8.
Step 11. Packaging your OCX
- Because of security settings on our machines, we cannot use the standard
packaging properties as it will not register your component correctly. We
will show you how to do this in lab. Once it is done, your control will be
available to any VB program on that machine.