![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Class Properties in Visual Basic 5 In Visual Basic 5, add-ins do not have a Public property. The Instancing variable determines both how the server provides objects and whether the objects can be created by external applications. For an add-in, the Instancing property should be set to MultiUse (5). This indicates both that external applications can create instances of the object and that a single server will provide multiple server objects. As is the case in Visual Basic 4, the server must ensure that multiple clients do not interfere with each other while accessing global variables. ConnectAddIn Visual Basic expects add-ins to implement two standard subroutines: ConnectAddIn and DisconnectAddIn. The Add-In Manager uses these routines to connect and disconnect the add-in server from the Add-Ins menu. When the Add-In Manager installs an add-in in the Visual Basic development environment, it invokes the add-ins ConnectAddIn subroutine. This routine should take whatever action is necessary to incorporate the server into the Visual Basic environment. Generally, this means placing new submenus and menu items in the Add-Ins menu. Figure 4.5 shows the Visual Basic 4 Add-Ins menu after the PropertySetter server has installed itself. PropertySetter adds a single menu command, Set Properties, to the Add-In menu.
Visual Basic provides a number of objects that the add-in can use to manipulate the programming environment. These objects are defined in a development environment module that the add-in project must reference. In Visual Basic 4, this is done using the References command in the Tools menu. In the References dialog, the Microsoft Visual Basic 4.0 Development Environment entry should be selected. In Visual Basic 5, the corresponding dialog is presented by the References command in the Project menu. In this dialog, the Microsoft Visual Basic 5.0 Extensibility reference should be selected. The ConnectAddIn subroutine is passed a single parameter of type VBIDE.Application. This object is a reference to the running Visual Basic development environment. The ConnectAddIn subroutine can access other objects such as the menus, forms, and controls in the current environment using this object. PropertySetters ConnectAddIn subroutine begins by saving a reference to the VBIDE.Application object for later use. Next the subroutine creates a new menu item in the Add-Ins menu. The VBIDE.Applications AddInMenu property is a reference to the Add-Ins menu object. That objects MenuItems property is a collection containing objects that describe the items in the menu. ConnectAddIn uses this collections Add function to create the new menu command. The Add function returns an object of type VBIDE.MenuLine. This object represents the new command line in the menu. VBIDE.MenuLine objects provide a function ConnectEvents that associates the menu item with an event handler object that should be activated when the item is selected. When the developer selects the item from the Add-Ins menu, Visual Basic invokes the event handler objects AfterClick subroutine. To respond to the menu item, the add-in should contain an event handler class that has an AfterClick subroutine. The ConnectAddIn subroutine should create an instance of this class and pass it to the menu items ConnectEvents function. Because the add-in server creates this object, not the client, the event handler class does not need to have its Instancing property set to allow outside applications to create event handler objects directly. In the PropertySetter application, the PropSet class provides the AfterClick event handler routine as well as the ConnectAddIn and DisconnectAddIn subroutines. Because PropertySetter provides only a single menu item, these three subroutines can all be contained by one class without causing confusion. A program, however, could implement an add-in that provided more than one command in the Add-Ins menu. In that case, it could create a different event handler class for each of the menu commands. The Scroller add-in described in Chapter 7 demonstrates how to build an add-in that provides more than one Add-Ins menu item. After the ConnectEvents function attaches the event handler object to the menu item, it returns a connection ID. The program should save this ID for later use. Subroutine DisconnectAddIn needs this ID to remove the add-in properly from the Add-Ins menu. The following code shows the ConnectAddIn subroutine used by the PropertySetter add-in. The global variables VBInstance, SetPropLine, and SetPropID are set by ConnectAddIn and are used later by DisconnectAddIn.
The environment instance.
Dim VBInstance As VBIDE.Application
The menu command line.
Dim SetPropLine As VBIDE.MenuLine
The event connection ID. This represents the
event handler to menu line connection.
Dim SetPropID As Long
Sub ConnectAddIn(vb_instance As VBIDE.Application)
Save the Visual Basic instance for later.
Set VBInstance = vb_instance
Add the command.
Set SetPropLine = _
VBInstance.AddInMenu.MenuItems.Add(&Set Properties...)
Connect the event handler object to the menu line.
SetPropID = SetPropLine.ConnectEvents(Me)
End Sub
DisconnectAddIn When the developer uses the Add-In Manager to remove an add-in from the Add-Ins menu, the Add-In Manager invokes the add-ins DisconnectAddIn subroutine. This routine should remove any menu items and commands created by the ConnectAddIn subroutine. PropertySetters PropSet class needs to perform only two tasks in DisconnectAddIn. First, it uses the event connection ID saved in subroutine ConnectAddIn to disconnect the event handler from the add-ins menu item. Second, it removes the menu item from the Add-Ins menu.
Sub DisconnectAddIn(mode As Integer)
Disconnect the event handler.
SetPropLine.DisconnectEvents SetPropID
Remove the command.
VBInstance.AddInMenu.MenuItems.Remove SetPropLine
End Sub
AfterClick The AfterClick subroutine provides the add-ins functionality. It can perform such tasks as creating new controls, moving controls around on a form, and adding new forms to the project. The PropertySetter add-in displays and sets property values for the selected controls. Its AfterClick subroutine is relatively simple. First, AfterClick sets the prop_set variable in the SetterDialog form to indicate the currently running PropSet class instance. The SetterDialog will use this instance later to obtain information about the controls that the developer has selected. Subroutine AfterClick then presents the SetterDialog. All of the rest of the work is performed by the SetterDialog.
Public Sub AfterClick()
Present the dialog.
Set SetterDialog.prop_setter = Me
SetterDialog.Show vbModal
Unload SetterDialog
End Sub
SetterDialog is also fairly simple. It uses functions provided by the PropSet class to perform all of the interesting work. For example, when the dialog is first loaded, it executes the following code to create the initial list of selected controls. Code to manipulate the selected controls is kept within the PropSet class, and SetterDialog deals exclusively with user interface issues.
Private Sub Form_Load()
Dim values As String
values = prop_setter.GetValues()
ValuesText.Text = values
End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|