Microsoft KB Archive/96941

= Using the Printer Object to Print a Grid Control's Contents =

Article ID: 96941

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



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
 Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. From the File menu, choose Add File. Select GRID.VBX. The Grid tool appears in the Toolbox. Place a grid (Grid1) on Form1. Set the Cols and Rows properties to 6.  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

  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    '   &quot; Dim x1 As Single    ' position of text Dim y1 As Single    '   &quot; Dim x2 As Single    ' position of grid lines Dim y2 As Single    '   &quot;

' 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

 Press the F5 key to run the program. Click Form1 to fill the grid with sample data and print the grid.</ol>

Additional query words: 1.00 2.00 3.00

Keywords: KB96941

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.