How to Create Column and Row Labels in VB Grid Custom Control

Last reviewed: June 21, 1995
Article ID: Q84113
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Professional Toolkit for Microsoft Visual Basic programming
  system for Windows, version 1.0

SUMMARY

The example program below demonstrates how you can display labels in the top row and left column of the Grid custom control at run time. It is not possible to assign labels in a grid at design time.

MORE INFORMATION

The example program below assigns labels to a grid from the Form_Load event procedure. It puts numbers down the left, labeling the first non-fixed row as "1". It puts letters across the top, labeling the first 26 non-fixed columns as "A" through "Z" then subsequent columns with "AA", "AB", and so on.

Steps to Create Example Program

  1. Run Visual Basic for Windows, or from the File menu, choose New Project (press ALT, F, N) if Visual Basic for Windows is already running. Form1 is created by default.

  2. From the File menu, choose Add File. In the Files box, select the GRID.VBX. The Grid tool appears in the Toolbox.

  3. Select the Grid tool from the Toolbox, and place a grid (Grid1) on Form1.

  4. On the Properties bar, set the Grid Cols and Rows properties to 30.

  5. Double-click the form to open the Code window. In the Procedure box, select Load. Enter the following code:

    Sub Form_Load ()
        Dim i As Integer
    
        ' Make sure grid has at least one fixed column and row.
        If Grid1.FixedCols < 1 Or Grid1.FixedRows < 1 Then
            Stop
        End If
    
        ' Put letters across top.
        For i = 0 To Grid1.Cols - 2
            Grid1.Col = i + 1
            Grid1.Row = 0
            Grid1.Text = Chr$(i Mod 26 + Asc("A"))
            ' If more than 26 columns, use double letter labels.
            If i + Asc("A") > Asc("Z") Then
                Grid1.Text = Chr$(i \ 26 - 1 + Asc("A")) + Grid1.text
            End If
            Grid1.FixedAlignment(Grid1.Col) = 2  ' Centered.
        Next
    
        ' Put numbers down left edge.
        For i = 1 To Grid1.Rows - 1
            Grid1.Col = 0
            Grid1.Row = i
            Grid1.Text = Format$(i)
        Next
        Grid1.FixedAlignment(0) = 2  ' Centered.
    End Sub
    
    

  6. Press the F5 key to run the program.


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsCus


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.