Creating an active X control
The simplest way to create the user interface of an Active X control is to use controls that already exist. This is known as creating from constituent controls. To illustrate the techniques for creating a control, we are going to create a simple security control that allows the user to enter a user ID and password. If you handle logins for your applications, or have to pass user information to a database, you at some time have had to ask for a user ID and password. Typically, you place a couple of labels and text boxes on a form and do a little bit of text processing. This is a simple enough task, but wouldn’t it be nice if all you had to do was draw a single control on the form, then access its properties to get all the needed information? This is what the security control does.
- Choose File> new project to open the new project window
- Click the Active X control icon. Visual Basic creates a new project that contain a user control, named user control 1 as shown in gif17.2 this is the control’s form on which the visible interface will be built. (once the project is created, you will need to set the name property of the control otherwise your control will go through life with the generic name user control (or 2 and so on)
- Click the control from the toolbox and place it on the User Controll.
- Set the basic properties of the control (name or the caption property for a label or command button)
Using the Active X control
After you have finished developing and resting your control, you can use the control in many programs. You can easily use the control in any other VBprogrma that you write. You can also use the control in any application that supports ActiveX controls. These applications include Microsoft office, other development tools such as FoxPro, and even internet applications or web pages that support ActiveX. With this type of usage available, you can see why the capability to create ActiveX control is such a powerful feature in Visual Basic.
Compiling the control
The first step to being able to use your control in other application is to complete it. When you compile a control, you are creating an ICX file that can be accessed by other programs. Compiling your control involves the following steps:
- Select the project in your project group that contains the control
- select he make OCX item form Visual Basic file menu
- From the make project dialog box, click the options button to set any necessary compilation options.
- IN the make project dialog box set the name and path for the OCX file.
- Click the OK button to start the compilation.
- When the process is finished, your will have an OCX file that others can use to access your control.
Working with the control in Visual Basic
To use your ActiveX control in other Visual Basic program, you have tow options:
- Your can use the OCX that contains the control.
- You can ad the CTL control definition file to the project.
Using the OCX for the control is the preferred method of accession the control unless you are going to modify the control. Even then, you are better off modifying the original control project and recompiling the control than making modifications in a new project. To add your OCX control to your Visual Basic toolbox, open the components dialog box by pressing Ctrl +T. Then click the browse button in the dialog to bring up the open dialog that allows you specify the name and path of the control. When you select the control, it will appear in the components dialog and will be market to be added to your toolbox. After closing the components dialog, your control will appear in the toolbox and will be accessible to the current project, like any other control.
Using the control in a web page
Internet explores 3.0 and 4.0, as well as some other internet programs are capable of displaying and using Active X controls. The key to using the control is the use of the object tag in the HTML code for the web page. The <object> tag specifies the following information about the control:
- The control’s ID
- A name for the instance of the control
- The class ID of the control
This information identifies the control to your web browser and allows the control to be displayed if the browser supports Active X. The following code shows one instance of the HTML code needed to embed an ActiveX control in a page.
<object id = “Test1” name “Test1”
classid = “clsid: 21F43727 – A8FD- 11D1 – BCCD- 0000C051F659”
border = “0”
width = “188” height = “54”></object>
web page designer make it easy to add Active X controls to your pages. These designers handle all the object information for you so you don’t have to remember all the details about class IDs and the other information.
Testing the active X control
Once you have created the ActiveX control, you need to test it make sure that all the procedures works as you think they should. You can use all the standard debugging techniques that are available in Visual Basic to debug your control. There are only a few different steps involved in setting up a test for your Active X control.
- Add a standard project to your development environment to create a project group, which is the mechanism used for testing Active X control and servers
- Close the user control window of your control project. If the user control (form) window is open, Visual Basic assumes that you are still working on the design of the control and will not allow you to create an instance of the control in another form
- Create an instance of the control on you test form. Then you can test the properties of the control and write code to test its methods.
Active X documents
Active X documents are designed the same way as the Visual Basic forms are designed. That is, they can have intrinsic controls and Active XC controls as the normal Visual Basic forma have. They can also show message boxes and secondary forms. But, in contrast to he independent appearance of the Visual Basic forms, Active X documents appear in containers, i.e internet browser windows such a Internet Explorer, Netscape navigator, etc. with Internet explorer 33.0 or later, active X document can be saved and ready but writing to and reading from the document’s data file.
The base object of an Active X document is the user Document object that resembles a standard Visual Basic form object with some exceptions.
The user document object has most but not all, of the events that are found on a form object. The events present on a form that are not found on the user document include.
- Activate
- Deactivate
- Load
- Query Unload
- Unload
Event present on the user document, but not found on a form object include.
- Enter focus
- Exit focus
- Hide
- Init properties
- Read properties
- Scroll
- Show
- Write properties
Active X documents can be packaged in either in process or out or process components |
Hyperlink objects
Hyperlink object behaves as a property of user documents. Using the properties and methods of the hyperlink object, the active X document can request a hyperlink aware container, such as Microsoft internet explorer, to jump to an another active X document.
This object has three methods:
- Navigate to
- Go back
- Go forward
Designing and compiling ActiveX Document
This section creates an Active X Document project called AcXDoc by demonstrating how to.
- Add user documents
- Ad forms
- Use hyperlink object
- Add menus to the user document
To create Act X Doc project
- Choose file | New project to open the new project dialog box.
- Double click the Active X document DLL icon. This will create an Active X document with default user document 1.
Active X Document DLL icon
The Active X document DLL project types will package the Active X documents as in process |
component. To package it as out of process component use Active X document EXE project
type
- Choose project | Add user document to add a second user document
- Repeat the above step to add third use document.
- Name them ActX Doc 1, ActDoc2 ActDoc3 respectively.
- Choose project | Add form to add a form and name it as form address.
Compiling your documents
After you have finished testing and debugging you document, you are ready to compile the document for distribution. To start the compilation process, select the make register. Ext menu option from the file menu of Visual Basic. This will then open the make project dialog box, prompting you the supply a name and location for the EXE or DLL file. The name of the .vdb files is based on the name property of the user document object. This file is placed in the same folder that you specified for the EXE file. After you have compiled the ActiveX document, you can view it in any container program that handles ActiveX document, you can view it in any container program that handles ActiveX document. Internet Explorer and office binder are two such programs. At the time this book is being written, Netscape navigator 4.0 does not support ActiveX documents. It is already mentioned that ActiveX documents can be built as either in process or out of process component. In both cases, when the project is compiled, in addition to creating an exe. Or a .dll file. Visual Basic creates a Visual Basic document file (Figure 18.5), which has the extension. Vbd and it is placed in the dame directory as the compiled component (.exe or .dll)
The .vbd file is actually OLE structured storage-this basically means that data in the file can be accessed and manipulated via standard OLE interfaces. The internet explorer uses this interface. So only instead of specifying. dob files are mentioned while calling navigate to method to hyperlink object to open another document.
Figure: compiling user documents produces.exe (or.dll) and .vbd files
Active X document migration wizard
The active X document migration wizard must be available to the Visual Basic IDE. If you have not already done so, you can make the wizard available by selecting if from the add in manager dialog box, which is accessible by choosing the add in manager item from the add in menu. After you have added the wizard to your desktop, you can urn it by choosing the ActiveX document migration wizard item from the add in a menu.
To begin the conversion process, the project whose4 forms you want to convert must be open. After you have opened the project, launch the ActiveX document migration wizard. At this point you will be presented with a series of screens that will guide you through the conversion process. The first of these screens in introductory only, so click the next button to continue.
The second screen of the wizard, shown in fig 18.6 includes a list box form which you can select the forms that you would like to convert. Any forms that have been defined for this project will be displayed in this list box. To select forms, simply click in the checkbox next to the name of the form. When you have made all your selections, click the next button to continue.
Figure: You can select multiple forms in the list box
The options page of the ActiveX document migration wizard, shown in Figure 18.7 lets you control how the wizard will process the forms you have selected. The three options enable you to do the following.
Figure: select the options that are appropriate to your needs
- Choose to comment out invalid code. This option will comment out statements such as load, unload, or end that are not supported by active X document.
- Remove original forms after conversion. This option will remove the forms form the current project after the conversion is made. Typically, you will not want to check this option because you will want your original project intact.
- Choose whether to convert your project to an ActiveX EXE or Active DLL project. The option defaults to Active EXE (Active XDLL are used for creating shared components rather than applications)
After making your choices, click the next button to continue. The last page of the wizard allows you to view a summary report that describes the wizard’s actions. After making your selection, click the finish button to begin the conversion. The summary report is a valuable development tool because it provides you with information regarding the steps you need to complete. Figure 18.8 shown the summary report.
Figure: The Active X Document migration wizard uses a summary report.
Viewing the wizard’s work
When the active X document migration wizard has finished its work, it place the newly created user document objects in the same project as the original forms. The document source files are stored in the same folder as the original form files and are given similar names, with the appropriate extension. For example, a form stored in the file frm Test .frm creates a user document stored in the file docTezt1. doc. As previously stated, the controls of the form are copied to the use document and their relative positions are preserved.
Also as stated previously, most of the code from your original form is copied over to the user document. “Invalid code is commented out and identified by the Active X document migration wizard.