Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:ActiveX Components
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
var end = document.cookie.indexOf (";", j);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(j, end));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
var m1='';
var gifstr=GetCookie("UsrType");
if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; }
document.write(m1+m2+m3);
Keyword
Title
Author
ISBN
Publisher
Imprint
Brief
Full
Advanced Search
Search Tips
Please Select
-----------
Components
Content Mgt
Certification
Databases
Enterprise Mgt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Prod Apps
Programming
Security
UI
Web Services
Webmaster
Y2K
-----------
New Titles
-----------
Free Archive
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!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98
function isIE4()
{
return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') );
}
function bookMarkit()
{
var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0";
parent.location.href=url;
//var win = window.open(url,"myitk");
//if(!isIE4())
// win.focus();
}
Search this book:
Previous
Table of Contents
Next
Initialization And Termination Procedures
Each class module has Class_Initialize and Class_Terminate procedures. These are executed when a client application creates an instance of the class and when the object is destroyed. You dont have to place any code in them. If your class requires any initialization or cleanup chores, however, this is where the code would go. If you want to see when your class is instantiated (when an object is created from it) and destroyed, you can place MsgBox calls in these procedures. Generally, however, code in these procedures should not interact with the user at all.
In our project, we need no cleanup, and during initialization our only task is to store the date and time in the private variable pDateCreated. Open the Class_Initialize procedure and add the single line of code shown in Listing 8.3.
Listing 8.3 The ActiveX servers Class_Initialize procedure.
Private Sub Class_Initialize()
pDateCreated = Now
End Sub
Now is a Visual Basic function that returns the current date and time. The next and final step in creating our ActiveX object is to define its one method.
Defining A Method
ActiveX classes can contain methods. These are essentially the same as the methods that are part of Visual Basic objects. A method is like a procedure: It is a chunk of code that performs some action. The action depends on the needs of the object. It can be just about anything that you can do in Visual Basic, from manipulating disk files to displaying information on the screen. In any case, the way you define a method is the sameby creating a Public procedure.
The single method this class needs is code to set the Name property, which could easily have been done using a Property Let procedure. By using a method instead, you will learn how to add methods to ActiveX objects. Use the Add Procedure command on the Tools menu to add a sub procedure named ChangeName (be sure you select Sub, not Property, as the type in the Add Procedure dialog box). Make sure that the Public option is selected. The code for this method is shown in Listing 8.4.
Listing 8.4 The ChangeName method.
Public Sub ChangeName(newName As Variant)
pObjName = newName
End Sub
The code is simple. The value passed as the method argument is assigned to the pObjName variable, which is associated with the Name property. By making this procedure public, we make it available as a method to clients. You can also define private procedures within an ActiveX object, but they can be called only from within the object itself.
Thats ityou can now compile and run your ActiveX server. First, select Make MyTestAXDLL from the File menu to create the DLL file on disk. Then select the Start With Full Compile command from the Run menu. You should always use this command (instead of Start) to run an ActiveX server from within the Visual Basic environment, because this improves your chances of catching errors during compilation rather than later when the server is called by a client. This command will first display the Debugging tab in the Project Properties dialog box, as shown in Figure 8.2. Be sure that the Wait For Components To Be Created option is selected, then click on OK.
Figure 8.2 Setting the DLLs debugging properties.
When it runs, the server wont do anything visible, but it will register your newly created class with Windows, making it available to the test program we are about to create. You should next terminate execution by selecting End from the Run menu, then minimize Visual Basic. Once the class has been registered, Windows knows that the class definition is located in the DLL file that you created. Next, Ill show you how to test the server.
Testing The ActiveX Server
To test our nice new ActiveX object, well need an ActiveX client. Well use Visual Basic to create this as well. Because Visual Basic is minimized with the DLL project loaded, how can we use it to create the ActiveX client? The trick is to start a second copy of Visual Basic and use the second instance to develop and test the client.
The DLL project does not need to be running in Visual Basic for this project to work, so why not just close the DLL project and start a new project for the client program? You could do this, but keeping a copy of Visual Basic in memory with the DLL project loaded will make it a lot easier to switch back and modify the DLL code if required.
When you start the second copy of Visual Basic, create a Standard EXE project. On the projects form, place a control array of four Command Buttons, plus two Text Boxes and two Labels. Figure 8.3 shows this form. Change the Label controls Caption properties to Value and Answer, as shown in the figure.
Assign a Name property of txtInput to the Text Box that is next to the Value label, and a Name property of txtOutput to the other Text Box. Make the Text property of both Text Boxes blank. Set the Caption properties of the Command Buttons as shown in Table 8.1.
Remember to save the project. I used the name TestAX for both the form and the project.
To use the class we just created, we must add a reference to the project, as with any object. Select References from the Project menu and scroll through the list until you find Calculates Square and Cube Roots, as shown in Figure 8.4. This is the Project Description we assigned for the ActiveX server projects Application Description project option. Click on the box next to this reference so that an X appears, then close the dialog box. Now, the ActiveX class that we created is available within the Visual Basic project.
Figure 8.3 The client programs form.
Table 8.1 Caption property settings for the array of Command Buttons.
Index
Caption
0
Square Root
1
Cube Root
2
Information
3
Exit
How did the description of our class come to be included in the References list? When you ran the DLL project in the first instance of Visual Basic, it registered itself with Windows. If the server is missing from the References list, you most likely forgot to run it from the other copy of Visual Basic. Note that selecting a reference in this list does not actually add the ActiveX object to the project, but merely makes it available to the project.
While its not a necessary step for your project to run, pressing F2 to open Visual Basics Object Browser will be instructive. The browser, shown in Figure 8.5, lets you view all of the classes that are registered and obtain information on their properties and methods. Scroll through the Classes list until you see AXDEMO, which is the name we assigned to our class. Highlight the name by clicking on it, and youll see the classs properties and methods displayed in the Members list. The Object Browser is a useful tool for investigating the properties and methods of the many classes that are registered on most Windows systems.
Figure 8.4 Adding a reference to the ActiveX class.
Figure 8.5 The Visual Basic Object Browser displaying the AXDEMO classs properties.
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. Read EarthWeb's privacy statement.
Wyszukiwarka
Podobne podstrony:
08 03 Uzgodnienia i pozwolenia na roboty w czynnych zakladach08 03 S1 W J ObcyCW Psychologia Poznawcza 08 03 0608 03 KPGO Spr z realizacji08 03 S1 W J ObcyNie wolno czekać aż będzie za późno (rozmowa z Richardem Perle 08 03 2003)EdW 08 03TI 03 03 08 T pl(1)TI 03 10 08 B pl(1)04 03 08 sem IIIwięcej podobnych podstron