Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


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. The result appears in Figure 20.6.


Figure 20.6  Designing the calculator ActiveX control.

Now double-click the command button to open its Click event handler:

Private Sub Command1_Click()

End Sub

When the user clicks the = button, we’ll just add the two values in Text1 and Text2, and place the sum in Text3 at the bottom. That looks like this in code:

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. How can we test it out? See the next topic.

Testing An ActiveX Control

You can test, and even debug, ActiveX controls in the Microsoft Internet Explorer, as long as you have version 3 or later. Just select the Start item in the Visual Basic Run menu to see the ActiveX control you’re designing at work.

Let’s see an example. Here, we’ll run the ActiveX control we developed in the previous topic, the calculator control. To open this control in the Internet Explorer, select the Project menu’s Calculator Properties item, opening the Project Properties dialog box, as shown in Figure 20.7. Click the Debugging tab now, as also shown in Figure 20.7.


Figure 20.7  Setting Visual Basic debugging options.

Leave the entry CalculatorControl in the Start Component box, and make sure the Use Existing Browser box is clicked, then click on OK. To run the control, just select the Run menu’s Start item now. This registers our control with Windows, creates a temporary HTML page with the control embedded in it, and starts the Internet Explorer, as you see in Figure 20.8.


Figure 20.8  Testing the calculator control in the Internet Explorer.

That’s it—now you can use the ActiveX control. In Figure 20.8, we’re adding numbers with the calculator. Our calculator ActiveX control is a success.

Creating A Visual Basic Project Group To Test An ActiveX Control

In the previous topic, we saw how to test an ActiveX control in a Web browser, but you can use ActiveX controls in Visual Basic programs too. Can you test an ActiveX control in Visual Basic?

Yes, you can, by creating a project group. How does that process work? As an example, we’ll see how to add our calculator ActiveX control (developed in the last two topics) to a program group. Just follow these steps:

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 (we used activexcalculatorcontrol.vbp in the previous topic), 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 (although you can give those files the names you want), including the group file itself, group1.vbg.
5.  Close the calculator project’s window (that is, the window in which you design the calculator by adding text boxes and so on—its designer 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.

Following these steps creates the running program you see in Figure 20.9. Now we’re testing ActiveX controls in Visual Basic.


Figure 20.9  Testing an ActiveX control in a program group.

Registering An ActiveX Control

The Testing Department is calling again. It’s all very well to be able to run ActiveX controls in a Visual Basic project group (see the previous topic), but wouldn’t it be nice to let other users use those ActiveX controls too?

To install an ActiveX control in Windows, you must register it with Windows, and you can do that either with the setup program (such as the ones we’ll create in Chapter 30), or with the Window regsvr32.exe utility. Let’s see an example. Here, we’ll see how to register an ActiveX control named, say, activex.ocx with Windows. 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 (which you open with the Project menu’s Components item), and you can add it to the Visual Basic toolbox.

Using A Custom ActiveX Control In A Visual Basic Program

Now that we’ve registered our Visual Basic control with Windows (see the previous topic), how do you add it to a form in a Visual Basic project? You add ActiveX controls that you build to Visual Basic projects just as you add any other ActiveX controls, such as the ones that come with Visual Basic.

Let’s see an example. After we register the calculator ActiveX control that we’ve developed over the previous few topics, that control will appear in the Visual Basic Components dialog box. Start a new standard EXE project now and open the Visual Basic Components dialog box by selecting the Project|Components item. Next, click the Controls tab in the Components dialog box, as shown in Figure 20.10.


Figure 20.10  Our ActiveX control appears in the Components dialog box.

Click the entry labeled calculator (“calculator” was the name we gave to the project when we created this control) in the Components dialog box to add our calculator, and close that dialog box to add the calculator to the Visual Basic toolbox. Now draw a new calculator ActiveX control in the program’s main form, creating the new control, CalculatorControl1 (CalculatorControl was the name we gave to the UserControl object when we created this control), and run the program, as shown in Figure 20.11.


Figure 20.11  Running our ActiveX control in a Visual Basic program.

That’s it—now we’ve created, registered, and added a functioning ActiveX control to a Visual Basic program.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.