Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitlePrint a FlexGrid control's data across multiple pages vertically and horizontally
DescriptionThis example shows how to print a FlexGrid control's data across multiple pages vertically and horizontally in Visual Basic 6.
KeywordsFlexGrid, print, grid, multi-page, multiple pages
CategoriesControls, Graphics
 
This is a fairly complex example so this page doesn't cover every detail. Download the code and take a look to get all the details.

Subroutine PrintFlexGridMultiPage takes as inputs a FlexGrid control, margins, and coordinates for displaying page numbers. It sets r = 0 and then calls PrintFlexGridRows. That routine increases r to represent the next row that has not been printed. PrintFlexGridMultiPage continues calling PrintFlexGridRows until all rows have been printed.

 
' Print a MSFlexGrid control's data across multiple pages
' vertically and horizontally.
Private Sub PrintFlexGridMultiPage(ByVal flx As MSFlexGrid, _
    ByVal l_margin As Single, ByVal t_margin As Single, _
    ByVal r_margin As Single, ByVal b_margin As Single, _
    ByVal page_x As Single, ByVal page_y As Single)
Dim r As Integer
Dim page_r As Integer

    ' Print the rows.
    r = 0
    page_r = 0
    Do While r < flx.Rows
        ' Print the next set of rows.
        PrintFlexGridRows flx, r, _
            l_margin, t_margin, r_margin, b_margin, _
            page_x, page_y, page_r
        page_r = page_r + 1
    Loop
End Sub
 
Subroutine PrintFlexGridRows prints rows in the FlexGrid. If there are too many columns to fit on a single page, it prints multiple pages arranged horizontally. It starts at row start_row and leaves start_row pointing to the next row to print when it is done.

The routine loops through the rows starting with start_row to see how tall they are and determine how many rows will fit on a page.

Next the routine sets c = 0 and calls subroutine PrintFlexGridRowColumns to print rows start_row through stop_row, starting at column c. When PrintFlexGridRowColumns returns, it sets c to the next column that has not been printed. Subroutine PrintFlexGridRows continues calling PrintFlexGridRowColumns until all of the columns have been printed.

 
' Print the horizontal pages for the next set of rows that
' will
' fit on a page vertically. Start with row start_row.
' Leave start_row pointing to the first row after those
' printed.
Private Sub PrintFlexGridRows(ByVal flx As MSFlexGrid, _
    ByRef start_row As Integer, ByVal l_margin As Single, _
    ByVal t_margin As Single, ByVal r_margin As Single, _
    ByVal b_margin As Single, ByVal page_x As Single, ByVal _
    page_y As Single, ByVal page_r As Integer)
Dim ymax As Single
Dim c As Integer
Dim stop_row As Integer
Dim page_c As Integer

    With flxData
        ' See which rows will fit on this page.
        ' Start with the first row.
        stop_row = start_row
        ymax = t_margin + .RowHeight(stop_row) + 2 * VGAP
        Do
            If stop_row + 1 >= .Rows Then Exit Do
            If ymax + .RowHeight(stop_row + 1) + 2 * VGAP > _
                b_margin Then Exit Do
            ymax = ymax + .RowHeight(stop_row + 1) + 2 * _
                VGAP
            stop_row = stop_row + 1
        Loop

        ' Print horizontal groups of pages.
        c = 0
        page_c = 0
        Do While c < .Cols
            PrintFlexGridRowColumns flx, start_row, _
                stop_row, c, _
                l_margin, t_margin, r_margin, b_margin, _
                page_x, page_y, page_r, page_c
            page_c = page_c + 1
        Loop
    End With

    ' Set start_row and page_r for return.
    page_r = page_r + 1
    start_row = stop_row + 1
End Sub
 
Subroutine PrintFlexGridRowColumns prints rows start_row through stop_row, starting at column start_col. First it loops through columns to see how many will fit on the page. It then prints those rows and columns, and draws lines between the cells.
 
' Print the horizontal pages for the rows start_row through
' stop_row
' starting with column start_col.
' Leave start_col pointing to the first column after those
' printed.
Private Sub PrintFlexGridRowColumns(ByVal flx As _
    MSFlexGrid, ByVal start_row As Integer, ByVal stop_row _
    As Integer, ByRef start_col As Integer, ByVal l_margin _
    As Single, ByVal t_margin As Single, ByVal r_margin As _
    Single, ByVal b_margin As Single, ByVal page_x As _
    Single, ByVal page_y As Single, ByVal page_r As _
    Integer, ByVal page_c As Integer)
Dim xmax As Single
Dim ymax As Single
Dim r As Integer
Dim c As Integer
Dim stop_col As Integer
Dim x As Single
Dim y As Single
Dim page_txt As String

    ' Print the page number.
    page_txt = "Page (" & Format$(page_r) & ", " & _
        Format$(page_c) & ")"
    Printer.CurrentX = page_x - Printer.TextWidth(page_txt)
    Printer.CurrentY = page_y
    Printer.Print page_txt;
    ' Debug.Print page_txt & ": ";

    With flxData
        ' See which columns will fit on this page.
        ' Start with the first column.
        stop_col = start_col
        xmax = l_margin + .ColWidth(stop_col) + 2 * HGAP
        Do
            If stop_col + 1 >= .Cols Then Exit Do
            If xmax + .ColWidth(stop_col + 1) + 2 * HGAP > _
                r_margin Then Exit Do
            xmax = xmax + .ColWidth(stop_col + 1) + 2 * HGAP
            stop_col = stop_col + 1
        Loop

        ' Print the cells that fit.
        ' Debug.Print start_row, stop_row, start_col,
        ' stop_col
        y = t_margin
        For r = start_row To stop_row
            x = l_margin
            For c = start_col To stop_col
                '(Debug) Printer.Circle (x, y), 40
                'Debug.Print "    (" & x + HGAP & ", " & y
                ' + VGAP & ")"
                Printer.CurrentX = x + HGAP
                Printer.CurrentY = y + VGAP
                Printer.Print .TextMatrix(r, c);
                x = x + .ColWidth(c) + 2 * HGAP
            Next c
            y = y + .RowHeight(r) + 2 * VGAP
        Next r

        ' Print a grid.
        ymax = y
        y = t_margin
        For r = start_row To stop_row
            '(Debug) Printer.Line (l_margin, y)-Step(60,
            ' 60), , B
            Printer.Line (l_margin, y)-(xmax, y)
            y = y + .RowHeight(r) + 2 * VGAP
        Next r
        Printer.Line (l_margin, y)-(xmax, y)

        x = l_margin
        For c = start_col To stop_col
            '(Debug) Printer.Line (x, t_margin)-Step(60,
            ' 60), , B
            Printer.Line (x, t_margin)-(x, ymax)
            x = x + .ColWidth(c) + 2 * HGAP
        Next c
        Printer.Line (x, t_margin)-(x, ymax)
    End With

    ' Finish this page.
    Printer.NewPage

    ' Set start_col for return.
    start_col = stop_col + 1
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated