home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Search this book:
 
Previous Table of Contents Next


The value of the rather lengthy variable VBInstance.ActiveProject.ActiveForm.SelectedControlTemplates contains a collection of the control templates for the currently selected controls, on the active form, in the active project, in the current instance of the Visual Basic development environment. Note that this collection does not hold the actual controls: It holds ControlTemplate objects that describe the controls.

ControlTemplate objects have a property named Properties. This is a collection of Property objects. The Property objects represent the actual properties of the controls themselves. The add-in can access a control’s property by using the property’s name as an index into the Properties collection just as it might index into any other collection. For example, to set the Name property of a control with template object control_template to “BigBox,” an add-in could use either of the following two statements:

control_template.Properties.Item(“Name”) = “BigBox”
control_template.Properties(“Name”) = “BigBox”

Using this collection of selected control templates, the add-in can perform operations on all of the selected controls. The following code aligns the selected controls on the left by setting all of their Left properties to 1440:

Dim ctl As VBIDE.ControlTemplate

    For Each ctl In _
        VBInstance.ActiveProject.ActiveForm.SelectedControlTemplates
            ctl.Properties(“Left”) = 1440
    Next ctl

The GetValues function in PropertySetter’s PropSet class uses a similar technique to obtain property values for the selected controls. It uses the On Error Resume Next statement to protect itself in cases where a control does not support the specified property. For example, this allows it to safely attempt to read the value of the Text property for a collection of controls even though some of the selected controls may not support the Text property.

The SetValues function is very similar. It first sets the value of the indicated property for all of the selected controls. Then it uses the GetValues function to return a revised list of the property values.

Public Function GetValues(prop_name As String) As String
Dim ctl As Object
Dim controls As VBIDE.SelectedControlTemplates
Dim txt As String
Dim value As String
Dim field_name As String
Dim idx As String

    GetValues = “”
    If NoControls() Then Exit Function

    Set controls = VBInstance.ActiveProject. _
        ActiveForm.SelectedControlTemplates

    txt = “”
    For Each ctl In controls
        field_name = ctl.Properties(“Name”)
        ‘ Deal with indexes if the control
        ‘ is part of a control array.
        idx = ctl.Properties(“Index”)
        If idx <> “-1” Then _
            field_name = field_name & _
            “(“ & idx & ”)”
        txt = txt & field_name & _
            Space$(20 - Len(field_name))

        On Error Resume Next
        value = ctl.Properties(prop_name)
        If Err.Number = 438 Then
            ‘ Property not supported.
            value = “”
        ElseIf Err.Number <> 0 Then
            ‘ Some other error.
            Beep
            MsgBox “Error reading property.” & _
                Error.Description, _
                vbOKOnly + vbInformation, _
                “Property Error”
            Exit Function
        End If
        On Error GoTo 0

        txt = txt & value & vbCrLf
    Next ctl
    GetValues = txt
End Function

Public Function SetValues(prop_name As String, _
    prop_value As String) As String
Dim ctl As Object
Dim controls As VBIDE.SelectedControlTemplates
Dim txt As String
Dim field_name As String
Dim idx As String

    SetValues = “”
    If NoControls() Then Exit Function

    Set controls = VBInstance.ActiveProject. _
        ActiveForm.SelectedControlTemplates

    For Each ctl In controls
        On Error Resume Next
        ctl.Properties(prop_name) = prop_value
        If Err.Number <> 0 And Err.Number <> 438 Then
            ‘ Some error other than an unsupported property.
            Beep
            MsgBox “Error setting property.” & _
                Error.Description, _
                vbOKOnly + vbInformation, _
                “Property Error”
            Exit Function
        End If
        On Error GoTo 0
    Next ctl
    SetValues = GetValues(prop_name)
End Function

These routines must be careful when accessing SelectedControlTemplates. This collection is deeply buried within objects contained in other objects contained in other objects. If any of the objects in the chain are not initialized, the program could run into trouble. For example, if no form is currently active, then VBInstance.ActiveProject.ActiveForm will have the value Nothing. In that case, the program cannot access the SelectedControlTemplates collection.

To avoid accessing the properties of an object that is not initialized, both GetValues and SetValues use function NoControls to decide if it is safe to access the SelectedControlTemplates collection. NoControls returns true if there are no controls selected and false otherwise.

Function NoControls() As Boolean
    ‘ Assume there will be trouble.
    NoControls = True

    ‘ Sneak up on the collection of selected
    ‘ controls so we don't try to access an
    ‘ object that is Nothing.
    If VBInstance.ActiveProject Is Nothing Then _
        Exit Function
    If VBInstance.ActiveProject.ActiveForm _
        Is Nothing Then _
        Exit Function
    If VBInstance.ActiveProject.ActiveForm. _
        SelectedControlTemplates.Count < 1 Then _
        Exit Function

    NoControls = False
End Function

Creating Sub Main

At this point, the add-in has a server class that can add items to the Add-In menu with the ConnectAddIn subroutine. It can remove those items with the DisconnectAddIn subroutine. The project also has one or more event handler classes with AfterClick subroutines. If the add-in supports a single command, as is the case with the PropertySetter add-in, the same class can handle all three functions.

The developer still cannot use the add-in because the Add-In Manager and the operating system do not know that the add-in exists.

The Add-In Manager learns about add-ins by examining an entry in an initialization file. In Visual Basic 4, this file is VB.INI. In Visual Basic 5, it is VBADDIN.INI. In Windows 95, these files are located in the computer’s Windows directory. In Windows NT, the files are in the WinNT directory.

If the server is a 16-bit application, an entry should be made in the “Add-Ins16” section of the initialization file. If the server is a 32-bit application, the entry belongs in the “Add-Ins32” section.

Entries in the initialization file give the Add-In Manager the names of the add-in applications and the classes that contain the ConnectAddIn and DisconnectAddIn subroutines. For example, suppose a project named MyProject contains an add-in class named Connector that provides the ConnectAddIn and DisconnectAddIn subroutines. Then the initialization file should contain an entry for a value named MyProject.Connector.


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-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.