Using the Printer Object to Print a Grid Control's Contents (96941)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 1.0
  • 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

This article was previously published under Q96941

SUMMARY

The example program in this article shows you how to print the contents of a grid control using the Printer object.

MORE INFORMATION

The example code prints a line border around the grid if the grid control BorderStyle is set to 1 and prints grid lines between the cells if GridLines is set to True.

Steps to Create Example Program

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running.
  2. From the File menu, choose Add File. Select GRID.VBX. The Grid tool appears in the Toolbox.
  3. Place a grid (Grid1) on Form1. Set the Cols and Rows properties to 6.
  4. Add the following code to the Form1 Click event:
       Sub Form_Click ()
          ' Add sample data to the grid
          Dim i, j
          For i = 0 To Grid1.Cols - 1
             For j = 0 To Grid1.Rows - 1
                Grid1.Col = i
                Grid1.Row = j
                Grid1.Text = Format$(i + j + i ^ j)
             Next
          Next
          ' Print the data
          Call Grid_Print(Grid1)
          Printer.EndDoc
       End Sub
    
    						
  5. Add the following code to the general declarations section:
       Sub Grid_Print (grid As Control)
          Dim tppx As Integer  ' alias TwipsPerPixelX
          Dim tppy As Integer  ' alias TwipsPerPixelY
          tppx = Printer.TwipsPerPixelX
          tppy = Printer.TwipsPerPixelY
          Dim Col As Integer   ' index to grid columns
          Dim Row As Integer   ' index to grid rows
          Dim x0 As Single     ' upper left corner
          Dim y0 As Single     '   "
          Dim x1 As Single     ' position of text
          Dim y1  As Single    '   "
          Dim x2  As Single    ' position of grid lines
          Dim y2  As Single    '   "
    
          ' set upper left corner
          x0 = Printer.CurrentX
          y0 = Printer.CurrentY
    
          ' draw the border around the grid
          If grid.BorderStyle <> 0 Then
             Printer.Line -Step(grid.Width - tppx, grid.Height - tppy), , B
             x0 = x0 + tppx
             y0 = y0 + tppy
          End If
    
          ' draw the text in the grid
          x1 = x0
          For Col = 0 To grid.Cols - 1
             ' skip non-visible columns
             If Col >= grid.FixedCols And Col < grid.LeftCol Then
                Col = grid.LeftCol
             End If
             ' stop if outside grid
             If x1 + grid.ColWidth(Col) >= grid.Width Then Exit For
             y1 = y0
             For Row = 0 To grid.Rows - 1
                ' skip non-visible columns
                If Row >= grid.FixedRows And Row < grid.TopRow Then
                   Row = grid.TopRow
                End If
                ' stop if outside grid
                If y1 + grid.RowHeight(Row) >= grid.Height Then Exit For
                ' set position to print the cell
                Printer.CurrentX = x1 + tppx * 2
                Printer.CurrentY = y1 + tppy
                ' print cell text
                grid.Col = Col
                grid.Row = Row
                Printer.Print grid.Text
                ' advance to next row
                y1 = y1 + grid.RowHeight(Row)
                If grid.GridLines Then
                   y1 = y1 + tppy
                End If
             Next
             ' advance to next column
             x1 = x1 + grid.ColWidth(Col)
             If grid.GridLines Then
                x1 = x1 + tppx
             End If
          Next
    
          ' draw grid lines
          If grid.GridLines Then
             x2 = x0
             y2 = y0
             For Col = 0 To grid.Cols - 1
                ' skip non-visible columns
                If Col >= grid.FixedCols And Col < grid.LeftCol Then
                   Col = grid.LeftCol
                End If
                x2 = x2 + grid.ColWidth(Col)
                ' stop if outside grid
                If x2 >= grid.Width Then Exit For
                Printer.Line (x2, y0)-Step(0, y1 - tppy)
                x2 = x2 + tppx
             Next
             For Row = 0 To grid.Rows - 1
                ' skip non-visible rows
                If Row >= grid.FixedRows And Row < grid.TopRow Then
                   Row = grid.TopRow
                End If
                y2 = y2 + grid.RowHeight(Row)
                ' stop if outside grid
                If y2 >= grid.Height Then Exit For
                Printer.Line (x0, y2)-Step(x1 - tppx, 0)
                y2 = y2 + tppy
             Next
          End If
       End Sub
    
    						
  6. Press the F5 key to run the program. Click Form1 to fill the grid with sample data and print the grid.

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