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


Chapter 5
Aligner

Aligner arranges controls in rows, columns, or both in the development environment. Architecturally Aligner is similar to PropertySetter. Both add-ins use a single class to implement the ConnectAddIn, DisconnectAddIn, and AfterClick subroutines. In the Aligner add-in, that class is named AlignHandler.

The first section in this chapter, “Using Aligner,” explains how this add-in works from an application developer’s point-of-view. It explains how a developer can use Aligner to arrange controls on a form quickly.

The “Key Techniques” section briefly lists the most important Visual Basic programming techniques used to implement Aligner. The rest of the chapter describes these techniques in detail.

Using Aligner

Aligner allows an application developer to arrange controls in rows, columns, or both. First the developer selects two or more controls on a form. When the developer selects the Align command from the Add-Ins menu, a dialog similar to the one shown in Figure 5.1 appears. Using the dialog’s tabs, the developer can specify row, column, or row and column arrangements. The controls on each tab allow the developer to specify arrangement details. For row alignment, the developer can indicate that the controls should be aligned at the top, middle, or bottom. For column alignment, the developer can specify right, left, or center alignment.

For row and column alignment, the developer can specify several parameters. These include the number of rows and columns, the spacing between rows and columns, and whether Aligner should make all rows and columns the same size.

Key Techniques

Chapter 4 explains how to create and install the PropertySetter add-in. Because Aligner’s architecture is similar to PropertySetter’s, its creation and installation are also similar, so those topics are not covered here.


FIGURE 5.1  The Aligner add-in.

This chapter shows how an add-in can use the Left, Top, Width, and Height properties of a group of selected controls to arrange them in rows and columns. Using similar techniques, an add-in could arrange controls in other ways such as the cascade or circle shown in Figure 5.2. (Note: Although not described in this book, the Circles Add-In is included in the compact disk’s Ch5\Circles directory. It was created using the AddInMaker application described in Chapter 7.)

The following list briefly describes the key techniques explained in this chapter. They are described in greater detail in the following sections.

•  Providing AfterClick. When the developer invokes an add-in, its AfterClick subroutine is triggered. This section describes Aligner’s AfterClick subroutine and shows how it calls other subroutines to arrange the controls appropriately.
•  Aligning Controls in Columns. Aligning controls in rows is very similar to aligning them in columns. This section explains how the Aligner add-in aligns controls in columns.
•  Aligning Controls in Rows and Columns. Aligning controls in rows and columns is a little more complicated than aligning them in columns. This section explains how Aligner arranges controls in rows and columns.


FIGURE 5.2  Controls arranged in a cascade and a circle.

Providing AfterClick

Aligner’s AlignHandler class provides the add-in’s ConnectAddIn, DisconnectAddIn, and AfterClick subroutines. ConnectAddIn and DisconnectAddIn are very similar to the PropertySetter add-in’s corresponding routines that were described Chapter 4 (they are not described here).

The AfterClick subroutine first makes sure the developer has selected at least two controls. If not, it presents a message indicating that the developer should do so before using Aligner. If at least two controls are selected, AfterClick presents the AlignerDialog form. This dialog allows the user to select different alignment options. If the user cancels the dialog, AfterClick does nothing more. Otherwise, it invokes the AlignControls subroutine to actually arrange the controls.

Public Sub AfterClick()

Dim no_controls As Boolean

    ‘ See if fewer than two controls are selected.
    If NoControls() Then
        no_controls = True
    ElseIf VBInstance.ActiveProject.ActiveForm. _
        SelectedControlTemplates.Count < 2 Then
            no_controls = True
    Else
            no_controls = False
    End If
    
    ‘ If fewer than two controls are selected, say so.
    If no_controls Then
        Beep
        MsgBox "Please select two or more controls before you start Aligner.", _
            vbOKOnly + vbInformation, _
            "No Controls Selected"
        Exit Sub
    End If
    
    ‘ Show the dialog and get the user’s choices.
    AlignerDialog.Show vbModal

    ‘ If the user canceled, we’re done.
    If AlignerDialog.Canceled Then Exit Sub
    
     Align the controls.
    AlignControls
End Sub

Subroutine AlignControls checks the type of alignment specified by the user and calls the AlignRow, AlignCol, or AlignRowsAndCols subroutine to arrange the controls appropriately.

Sub AlignControls()

    ‘ See what kind of alignment the user wants.
    Select Case AlignerDialog.Tabs.Tab
        Case ALIGN_ROW          ‘ Align as a row.
            AlignRow
        Case ALIGN_COL          ‘ Align as a column.
            AlignCol
        Case ALIGN_ROW_AND_COL  ‘ Align as rows and columns.
            AlignRowsAndCols
    End Select
End Sub

Aligning Controls in Columns

The AlignCol subroutine performs most of the work of arranging the selected controls in a column. AlignRow is very similar so it is not described here. You can find its complete source code in the Ch5 directory on the compact disk.

AlignCol begins by determining the X coordinate to which the controls should be aligned. If the developer specifies left alignment, the X coordinate used is the value of the controls’ smallest Left property. If the developer specifies right alignment, the X coordinate used is the largest value of the controls’ Left + Width properties. Finally, if the developer specifies centered alignment, the X coordinate is set to the average of the values Left + Width / 2 over all the controls.


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.