|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97
Part Two Add-Ins
An add-in is a special kind of application that a developer can incorporate directly into the Visual Basic development environment. The Add-In Manager installs add-ins in the environments Add-Ins menu. The developer can then call on the add-ins to make routine programming chores easier.
Chapter 4 describes the PropertySetter add-in. PropertySetter allows an application developer to examine and modify the properties of several controls at the same time. This is particularly useful for changing properties, such as the Name property, that the Visual Basic development environment will not change in a group.
Chapter 5 explains the Aligner add-in. Aligner lets a developer easily arrange the controls on a form in rows, columns, or grids. Using similar techniques an add-in could arrange controls in other ways such as in circles, cascading over each other, or tiled to fill the form without overlapping.
The ExpenseReporter application described in Chapters 1 and 2 uses scrolling regions to manage more information than will fit on the screen at one time. Chapter 6 describes the Scroller add-in that allows a developer to quickly create similar scrolling regions in other applications.
Creating an add-in is a simple but detailed task. Add-ins are designed to automate such tasks. Chapter 7 describes AddInMaker, an add-in that automates the creation of other add-ins. Using AddInMaker a developer can create other add-ins quickly and easily.
Chapter 4 PropertySetter
PropertySetter allows an application developer to examine and modify the properties of many controls all at once. This is particularly helpful for changing properties that the Visual Basic development environment will not change in a group.
For instance, Visual Basic will change the Name property of more than one control at one time. Suppose an application includes a control array named Address that contains 50 controls. Changing the arrays name to StreetAddress is a lot of work. The developer must select each of the 50 controls individually and change their names one by one. PropertySetter allows the developer to select all of the controls and change their names in a single step.
A developer must install an add-in before it can be used. The following section, Installing PropertySetter, explains how to place an add-in in the Add-Ins menu using the Add-Ins Manager.
The next section, Using PropertySetter, describes this add-ins main features from the users point-of-view. It explains how a developer can use PropertySetter to examine and modify property values in the design environment.
The Key Techniques section briefly lists the most important Visual Basic programming methods used to implement PropertySetter. The rest of the chapter describes these methods in detail.
Unfortunately, many of the details of creating an add-in are different in Visual Basic 4 and Visual Basic 5. The following sections explain the differences as they describe the different steps required to build an add-in.
Installing PropertySetter
Before an add-in can be used, it must be installed in the development environments Add-Ins menu. This section describes the installation process from the developers point-of-view. It does not explain the steps necessary to prepare the add-in for installation. Those steps are described in later sections.
To install the PropertySetter add-in, the developer first selects the Add-In Manager command from Visual Basics Add-Ins menu. Figure 4.1 shows the Add-In Manager. After selecting the PropertySetter add-in, the developer clicks the OK button and the Add-In Manager installs the add-in.
Removing an add-in is just as simple. The developer opens the Add-In Manager, removes the check in the box next to the add-ins name, and presses the OK button.
Using PropertySetter
The ExpenseReporter and AppointmentBook applications described in Chapters 1, 2, and 3 are designed for use by typical business users. Add-ins, on the other hand, are designed to assist application developers. The end users of these programs are Visual Basic programmers.
This section explains how an application developer can use the PropertySetter add-in, assuming the add-in is correctly built and installed. Later sections explain how to build an add-in.
Before using PropertySetter, the developer should select a group of controls on a form in the Visual Basic development environment. The Set Properties command in the Add-Ins menu will then activate PropertySetter, displaying a dialog similar to the one shown in Figure 4.2. In this example, the developer selected four controls called NameLabel, CmdOk, AddressLabel, and CmdCancel.
The developer can then enter the name of a property in the Property text box. Pressing the Get Values button makes PropertySetter display the selected controls property values. In Figure 4.2, PropertySetter displays the values of the controls Caption properties.
To change a property value, the developer should enter the property name as before and place the new value in the New Value text box. When the developer clicks the Set Values button, PropertySetter updates the property for each of the selected controls.
FIGURE 4.1 The Add-In Manager.
FIGURE 4.2 The PropertySetter add-in.
Both the Get Values and Set Values commands work only for controls that actually have the indicated property. For example, Figure 4.2 shows PropertySetter with two labels and two command buttons selected. Suppose the developer enters Default into the Property text box and clicks Get Values. Because command buttons have a Default property but text boxes do not, PropertySetter will return values only for the command buttons.
PropertySetter provides four menu commands. The File menus Close command closes PropertySetter. The Get Values and Set Values commands on the Properties menu duplicate the functions of the command buttons. They are provided as menu commands for convenience, and they support the shortcut keys Ctrl-G and Ctrl-S.
The Help menus About PropertySetter command presents an about dialog. Like other about dialogs, this one gives version and copyright information. It also gives a brief explanation of how to use PropertySetter. This gives PropertySetter some simple help with a minimum of effort.
Key Techniques
This chapters main purpose is to explain how to create and install a basic add-in. While PropertySetter itself is a simple program, it demonstrates useful techniques for accessing the controls selected in the development environment. It also shows how an add-in can read and modify the properties of those controls. The key techniques described in this chapter are listed briefly below. They are described in detail in the following sections.
- Understanding OLE. Add-ins are OLE servers used by the Visual Basic programming environment. This section explains how add-ins can provide OLE services to client applications.
- Creating Add-Ins. Creating an add-in is a complicated process. This section describes the steps needed to create and install an add-in using Visual Basic 4 and Visual Basic 5. This section ends with a summary of the steps required.
- Testing Add-Ins. Because add-ins are invoked by the Visual Basic development environment, testing them is different from testing other applications. This section explains how the add-in designer can use two instances of the Visual Basic development environment to test an add-in.
Understanding OLE
Object Linking and Embedding, or OLE, is a standard that defines how applications should use objects to interact with each other. Starting with the release of Visual Basic 5, Microsoft has changed the name OLE to ActiveX. For most purposes, the two names are interchangeable. In order to understand OLE, one must first understand classes and objects.
Version 4.0 of Visual Basic introduced classes to the language. In many ways, a class is similar to a form that has no user interface. Like a form, a class can have variables, subroutines, functions, and property procedures. Variables, subroutines, and functions can be private to the class module or public so other parts of an application can interact with them.
After a class is defined, an application can create instances of the class, much as it would create new instances of a form. If the class MyClass is defined, the program can create new instances using either of the following two methods:
Dim instance_1 As New MyClass Declare and initialize instance_1.
Dim instance_2 As MyClass Declare instance_2.
Set instance_2 = New MyClass Initialize instance_2.
Once a program has created an instance of a class, it can access the public variables, subroutines, functions, and property procedures of the instance much as it would access similar objects for a form. If the object my_obj belongs to a class that has defined a public subroutine FactorAll, the program could invoke the objects FactorAll subroutine, as in my_obj.FactorAll.
|