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 the values in a ListView control across multiple pages
DescriptionThis example shows how to print the values in a ListView control across multiple pages in Visual Basic 6.
KeywordsListView, print, multipage, multi-page
CategoriesControls, Graphics
 
The PrintMultiPageListView subroutine measures the text in the ListView's column headers and its columns to see how wide each column must be.

The Boolean need_top indicates whether the code needs to print the top column headers. The Boolean start_new_page indicates whether the code needs to use Printer.NewPage to start a new page. Initially need_top is True and start_new_page is False.

The code loops through the items in the ListView. For each item, it checks need_top. If need_top is True, the code prints the column headers. If start_new_page is TRue, the code uses Printer.NewPage to start a new page.

Next the code prints the item's text and its subitem values.

The code then increments the Y coordinate for the next line. If the new value is greater than the bottom margin value, then the program draws a box around everything and draws lines between the columns. It then sets need_top and start_new_page to True so the next pass through the loop starts a new page.

After it has printed every item, the code checks start_new_page. If start_new_page is True, then the loop ended the page right when it displayed the last item. In that case, the code still needs to draw a box around everything and lines between the columns.

 
Private Sub PrintMultiPageListView(ByVal lvw As ListView)
' Use 1 inch margins.
Const TOP_MARGIN = 1440
Const LEFT_MARGIN = 1440
Const COL_MARGIN = 240

Dim bottom_margin As Single
Dim xmax As Single
Dim num_cols As Integer
Dim column_header As ColumnHeader
Dim list_item As ListItem
Dim i As Integer
Dim num_subitems As Integer
Dim col_wid() As Single
Dim X As Single
Dim Y As Single
Dim line_hgt As Single
Dim need_top As Boolean
Dim start_new_page As Boolean

    bottom_margin = Printer.ScaleTop + _
        Printer.ScaleHeight - TOP_MARGIN

    ' Get column widths.
    num_cols = lvw.ColumnHeaders.Count
    num_subitems = num_cols - 1
    ReDim col_wid(1 To num_cols)

    ' Check the column headers.
    For i = 1 To num_cols
        col_wid(i) = _
            Printer.TextWidth(lvw.ColumnHeaders(i).Text)
    Next i

    ' Check the items.
    num_subitems = num_cols - 1
    For Each list_item In lvw.ListItems
        ' Check the item.
        If col_wid(1) < Printer.TextWidth(list_item.Text) _
            Then _
           col_wid(1) = Printer.TextWidth(list_item.Text)

        ' Check the subitems.
        For i = 1 To num_subitems
            If col_wid(i + 1) < _
                Printer.TextWidth(list_item.SubItems(i)) _
                Then _
               col_wid(i + 1) = _
                   Printer.TextWidth(list_item.SubItems(i))
        Next i
    Next list_item

    ' Add a column margin.
    xmax = LEFT_MARGIN
    For i = 1 To num_cols
        col_wid(i) = col_wid(i) + COL_MARGIN
        xmax = xmax + col_wid(i)
    Next i

    ' Get a line's height.
    line_hgt = Printer.TextHeight("X")

    ' Print the pages.
    need_top = True         ' Start printing a new page.
    start_new_page = False  ' Use the NewPage method.
    For Each list_item In lvw.ListItems
        ' See if we need to print the top.
        If need_top Then
            need_top = False

            ' Start a new page if necessary.
            If start_new_page Then Printer.NewPage
            start_new_page = False

            ' Print the column headers.
            Printer.CurrentY = TOP_MARGIN + line_hgt / 2
            X = LEFT_MARGIN + COL_MARGIN / 2
            For i = 1 To num_cols
                Printer.CurrentX = X
                Printer.Print FittedText( _
                    lvw.ColumnHeaders(i).Text, col_wid(i));
                X = X + col_wid(i)
            Next i
            Printer.Print

            Y = Printer.CurrentY + line_hgt / 2
            Printer.Line (LEFT_MARGIN, Y)-(xmax, Y)
            Y = Y + line_hgt / 2
        End If

        ' Print the next item.
        ' Print the item.
        X = LEFT_MARGIN + COL_MARGIN / 2
        Printer.CurrentX = X
        Printer.CurrentY = Y
        Printer.Print FittedText(list_item.Text, _
            col_wid(1));
        X = X + col_wid(1)

        ' Print the subitems.
        For i = 1 To num_subitems
            Printer.CurrentX = X
            Printer.Print FittedText( _
                list_item.SubItems(i), col_wid(i + 1));
            X = X + col_wid(i + 1)
        Next i
        Y = Y + line_hgt * 1.5

        ' See if we've reached the bottom of the page.
        If Y >= bottom_margin Then
            ' We've reached the bottom.
            ' Draw lines around it all.
            Printer.Line (LEFT_MARGIN, TOP_MARGIN)-(xmax, _
                Y), , B

            X = LEFT_MARGIN
            For i = 1 To num_cols - 1
                X = X + col_wid(i)
                Printer.Line (X, TOP_MARGIN)-(X, Y)
            Next i

            ' Start a new page for the next item.
            need_top = True
            start_new_page = True
        End If
    Next list_item

    ' See if we finished the last page.
    If Not start_new_page Then
        ' We did not finish the last page.
        ' Draw lines around it all.
        Printer.Line (LEFT_MARGIN, TOP_MARGIN)-(xmax, Y), , _
            B

        X = LEFT_MARGIN
        For i = 1 To num_cols - 1
            X = X + col_wid(i)
            Printer.Line (X, TOP_MARGIN)-(X, Y)
        Next i
    End If
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated