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


After determining the alignment X value, AlignCol modifies each of the selected controls’ Left properties to align the controls as required.

Sub AlignCol()

Dim the_controls As VBIDE.SelectedControlTemplates
Dim ctl As Object
Dim x As Single
Dim wid As Single
Dim test_x As Single
Dim test_wid As Single

    Set the_controls = VBInstance.ActiveProject _
        .ActiveForm.SelectedControlTemplates
    
    ‘ Align the controls appropriately.
    If AlignerDialog.ColOption(ALIGN_LEFT).value Then
        x = the_controls.Item(0).Properties("Left")
        For Each ctl In the_controls
            test_x = ctl.Properties("Left")
            If x > test_x Then x = test_x
        Next ctl
            
        For Each ctl In the_controls
            ctl.Properties("Left") = x
        Next ctl
    ElseIf AlignerDialog.ColOption(ALIGN_CENTER).value Then
        x = 0
        wid = 0
        For Each ctl In the_controls
            test_x = ctl.Properties("Left")
            test_wid = ctl.Properties("Width")
            x = x + test_x
            wid = wid + test_wid
        Next ctl
            
        x = (x + wid / 2) / the_controls.Count
        For Each ctl In the_controls
            ctl.Properties("Left") = x - _
                ctl.Properties("Width") / 2
        Next ctl
    Else
        x = the_controls.Item(0).Properties("Left") + _
            the_controls.Item(0).Properties("Width")
        For Each ctl In the_controls
            test_x = ctl.Properties("Left") + _
                     ctl.Properties("Width")
            If x < test_x Then x = test_x
        Next ctl
            
        For Each ctl In the_controls
            ctl.Properties("Left") = x - _
                ctl.Properties("Width")
        Next ctl
    End If
End Sub

Aligning Controls in Rows and Columns

Aligner’s AlignRowsAndCols subroutine is more complicated than subroutine AlignCol. Figure 5.3 shows the AlignerDialog tab that allows the developer to specify row and column arrangements. Using this dialog, the developer can indicate whether rows should be aligned at the top, middle, or bottom and whether columns should be aligned on the left, center, or right. It also allows the developer to specify the number of rows and columns, the spacing Aligner should use between rows and columns, and whether rows and columns should be given the same sizes.

AlignRowsAndCols needs to arrange the selected controls in a specific order. It must place the controls in rows and columns starting with those at the upper right and moving down toward the lower left. This will place the controls as close to their original positions as possible. Generally, controls that begin on the left will still be on the left in the new arrangement.

The SelectedControlTemplates collection does not provide an Add method, so sorting the control templates within this collection is difficult. The add-in cannot add and remove control templates to sort them within SelectedControlTemplates. To overcome this problem, AlignRowsAndCols first copies the control templates into an array that it can more easily manipulate.


FIGURE 5.3  Specifying row and column alignment.

At the same time, the subroutine creates arrays that contain the Left and Top properties of each control. These values are used frequently in the following calculations, and storing them in arrays is much faster than repeatedly accessing them through the control templates’ Properties collections.

Next AlignRowsAndCols uses subroutine SortControls to rearrange the control templates in the controls array. SortControls first sorts the templates so those with the smallest Top values come first. This will make it easy to place those controls in the upper rows of the new arrangement. SortControls then sorts the templates within each row so the controls with the smallest Left values come first.

After sorting the control templates, AlignRowsAndCols uses subroutine ArrangeRowColControls to place the controls in their new positions. This subroutine examines the controls in each row to determine how tall the row must be. Similarly, it examines the controls in each column to see how wide each column must be. If the developer checks the Same sized rows or Same sized columns boxes on the AlignerDialog, ArrangeRowColControls uses the largest row and column size for every row and column, respectively.


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.