![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
But now, lets get back to the client program. In the forms General declarations section, we create a reference to the ActiveX class by declaring a variable of that type, as shown here: Private Ob1 As New AXDEMO Note the use of the New keyword, which is necessary, because we want to create a new instance of AXDEMO to which the variable Ob1 will refer. Without New, we could make Ob1 refer to only an existing AXDEMO object, using the Set statement. Of course, that existing instance of AXDEMO would need to be created earlier with New. Note also that the AXDEMO class is available in the Auto list that Visual Basic displays after you type the As keyword. This is an excellent demonstration of how Visual Basic is well integrated to make your life as a programmer as easy as possible. Declaring a variable of type AXDEMO does not actually create an instance of the class. This occurs the first time a property or method of the class is referenced. Well do this in the client apps Form_Load procedure by using the ChangeName method to assign a name to the servers Name property: Private Sub Form_Load() Ob1.ChangeName(MyObject) End Sub The one line of code in this procedure calls the ChangeName method in Ob1. As we saw earlier, Ob1 refers to an instance of AXDEMO. Code in the ChangeName procedure assigns the argument (in this case MyObject) to the private variable objName. Having the object perform the calculations is a simple matter. Clicking on either the Cube Root or Square Root button assigns the number in the first Text Box to the objects Number property. Because the code that performs the calculations is located in the Number propertys Let procedure, it is performed automatically each time this property is changed. We can then immediately read back the answer from the appropriate object propertyeither CubeRoot or SquareRoot. To obtain information about the object, we access its Name and Created properties, placing them together in a string that is displayed with the MsgBox procedure. To terminate the program, we destroy the AXDEMO object by setting Ob1 to the special keyword Nothing, then call End to terminate. Listing 8.5 shows the code for the Click event procedure for the array of Command Buttons. Listing 8.5 The Click event procedure for the demo programs Command Buttons. Private Sub Command1_Click(Index As Integer) Dim Msg As String Select Case Index Case 0 Square root Ob1.Number = txtInput.Text txtOutput.Text = Ob1.SquareRoot Case 1 Cube root Ob1.Number = txtInput.Text txtOutput.Text = Ob1.CubeRoot Case 2 Information Msg = Object & Ob1.Name & was created _ & Ob1.Created MsgBox (Msg) Case 3 Quit Set Ob1 = Nothing End End Select End Sub Thats all there is to the client app. Run it, enter a value in the first Text Box, and click on one of the calculation buttons. Because the program has no error checking, be sure to enter a positive valueno negative numbers, please. Click on the Information button to see the objects name and creation time/date. What if you need to make changes in the DLL code? Switch to the first copy of Visual Basic and make the changes. Be sure to execute the DLL within Visual Basic (to register any changes) and to use the Make command on the File menu to create the DLL file on disk. Then you can switch to the second copy of Visual Basic and run the client program again to see the effects of your changes.
Registering ComponentsAs you have seen, for an ActiveX component to be available to other programs, it must be registered with Windows. If you run an ActiveX server from within the Visual Basic environment, it is automaticallybut temporarilyadded to the Windows registry. When you terminate execution, the registry entry vanishes. If you make the project, creating an EXE or DLL file, the server and its classes are permanently added to the registry. But what about end users who are not running Visual Basic? For them, the class is registered when it is installed by using the Setup tool kit, or when it is executed as a regular EXE file. It can also be registered using the Windows REGSVR32.EXE utility. Creating An ActiveX EXEWe have seen how to create an ActiveX component contained within a DLL file. You could include more than one ActiveX class in a single DLL simply by adding additional class modules to the project. But what about an ActiveX EXE? As I mentioned earlier in the chapter, an ActiveX EXE combines one or more ActiveX components with a functional program. In almost all cases, the functional part of the ActiveX EXE makes use of its own ActiveX components as well as exposing them for use by other programs. How do you do this? Its really quite simple. I wont walk you though a complete example, but I will describe the process using our earlier demonstration programs for examples. Suppose you want to combine our ActiveX server with the client demonstration to create an ActiveX EXE that does the following:
You can easily accomplish these tasks by creating an ActiveX EXE project. The project will have two modules: a class module containing the same code as the class module in our DLL example, and a regular Visual Basic form module with the same controls and code as the client example. In this case, the class and its client are part of the same Visual Basic project, and the class is also available (once it has been registered) for use by other Windows programs.
|
![]() |
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. Read EarthWeb's privacy statement. |