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



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 2.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0
  • Microsoft Visual Basic Professional Edition for Windows 2.0
  • Microsoft Visual Basic Professional Edition for Windows 3.0
  • Microsoft Professional Toolkit for Microsoft Visual Basic programming system for Windows

This article was previously published under Q84113

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.

Modification Type:MajorLast Reviewed:12/12/2003
Keywords:KB84113