|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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.
Now double-click the command button to open its Click event handler: Private Sub Command1_Click() End Sub When the user clicks the = button, well 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 thats itweve created a new calculator ActiveX control. How can we test it out? See the next topic. Testing An ActiveX ControlYou 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 youre designing at work. Lets see an example. Here, well run the ActiveX control we developed in the previous topic, the calculator control. To open this control in the Internet Explorer, select the Project menus 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.
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 menus 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.
Thats itnow you can use the ActiveX control. In Figure 20.8, were adding numbers with the calculator. Our calculator ActiveX control is a success. Creating A Visual Basic Project Group To Test An ActiveX ControlIn 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, well see how to add our calculator ActiveX control (developed in the last two topics) to a program group. Just follow these steps:
Following these steps creates the running program you see in Figure 20.9. Now were testing ActiveX controls in Visual Basic.
Registering An ActiveX ControlThe Testing Department is calling again. Its all very well to be able to run ActiveX controls in a Visual Basic project group (see the previous topic), but wouldnt 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 well create in Chapter 30), or with the Window regsvr32.exe utility. Lets see an example. Here, well see how to register an ActiveX control named, say, activex.ocx with Windows. First, use the File menus Make activex.ocx menu item to create activex.ocx. Next, well use regsvr32.exe, which is usually found in the C:\windows\system directory, to register that control with Windows. Heres 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 menus Components item), and you can add it to the Visual Basic toolbox. Using A Custom ActiveX Control In A Visual Basic ProgramNow that weve 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. Lets see an example. After we register the calculator ActiveX control that weve 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.
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 programs 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.
Thats itnow weve created, registered, and added a functioning ActiveX control to a Visual Basic program.
|
|
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. |