Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Expand Your VB6 Printing Repertoire—Part III : Page 3

Part III of this series shows you how to use the ListView control in order to display tabular data, then provides a simple tool you can use to print that data.


advertisement

The PrintListView Procedure
All the action in this example takes place from within the subroutines and the PrintListView subroutine is the core of the application. The subroutine itself is quite lengthy, so only its core code is discussed here. You can open the projectin your Visual Basic editor, examine the code and adjust it for your project needs.

The following code iterates through the rows of the control (the ListItemscollection). For each row, it prints the item's text and then the text of each subitem on the same row. Here are the two basic loops (I've omitted the statements that advance the current location after printing each cell):


Print the ListView control’s items
Private Sub PrintListView()
    Dim row As Integer, col As Integer
    Dim txtLines() As String
    ' Iterate through the ListItems collection
    For row = currentRow To LV.listitems.Count - 1
        CellHeight = RowHeight
        txtLines = Split(BreakItemText(LV.listitems(row + 1).Text, _
                                        colWidths(0) - 2), vbCrLf)
        PrintSubItems txtLines, 0
        XPos = XPos + colWidths(0)
        ' Then iterate through the current item's subitems ...
        For col = 1 To LV.ColumnHeaders.Count - 1
            If col <= LV.listitems(row + 1).listsubitems.Count Then 
                txtLines = Split(BreakItemText( _
                           LV.listitems(row + 1).listsubitems(col), _
                           colWidths(col) - 2), vbCrLf)
                PrintSubItems txtLines, col
            End If
        Next
        ' Print a horizontal line between cells
        PRN.Line (LeftMargin, YPos + 0)- _
                 (LeftMargin + TotalWidth, YPos + 0), GridColor
        ' Is there room for another item?
        If PRN.CurrentY > TopMargin + PageHeight - 2 * RowHeight Then
            PRN.Line (LeftMargin, YMin)-(LeftMargin, YMax), GridColor
            DrawVerticalLines
            PageNo = PageNo + 1
            If PRN Is Printer Then PRN.NewPage
            Exit Sub
        End If
        currentRow = row + 1
    Next
    PRN.Line (LeftMargin, YMin)-(LeftMargin, YMax), GridColor
    DrawVerticalLines
    MorePages = False
    If PRN Is Printer Then PRN.EndDoc
End Sub


The code is straightforward, except for the statements that break the current cell's text in multiple lines. The BreakItem function accepts as arguments the string to be printed and the width in which it should fit, and breaks it into multiple text lines. The BreakItemText function, along with the GetNextWord helper function, is shown in the next code block. The array of text lines to be printed, along with the current column's index, is passed as argument to the PrintSubItems subroutine, which prints the current cell. Its code is straightforward, because it knows the coordinates of the cell's upper left corner (the statements that manipulate these two coordinates are not shown in this article's listings for brevity). The PrintSubItems subroutine prints the text lines of each cell taking into consideration the alignment of the text on the corresponding column on the ListViewcontrol.


Printing a row's subitems
Private Sub PrintSubItems(lines() As String, ByVal col As Integer)
    Dim i As Integer
    For i = 0 To UBound(lines)
        If PRN.CurrentY < TopMargin + PageHeight Then
            lines(i) = Trim(lines(i))
            ' LEFT ALIGNED COLUMN
            If LV.ColumnHeaders(col + 1).Alignment = _
                                    lvwColumnLeft Then
                PRN.CurrentX = XPos + extraHSpace
                PRN.Print lines(i)
                ' RIGHT-ALIGNED COLUMN
                ElseIf LV.ColumnHeaders(col + 1).Alignment = _
                                    lvwColumnRight Then
                    PRN.CurrentX = XPos + (colWidths(col) - _
                                    PRN.TextWidth(lines(i)) - 1)
                    PRN.Print lines(i)
                    Else
                        ' CENTERED COLUMN
                        PRN.CurrentX = XPos + _
                               (colWidths(col) - _
                               PRN.TextWidth(lines(i)) - 2) / 2 + 1
                        PRN.Print lines(i)
            End If
        Else
            PRN.CurrentX = XPos + extraHSpace
            PRN.Print "* * *"
            Exit For
        End If
    Next
    If CellHeight < PRN.CurrentY - YPos Then
        CellHeight = PRN.CurrentY - YPos
    End If
End Sub

The code assumes that no cell is too tall to cause any problems; in other words, it doesn't break a tall row between two consecutive pages. Each item of the ListItemscollection will be printed on the same page. Should one of the cells become too long, the code detects this condition and skips the part of the item that doesn't fit comfortably on the page (it actually prints three stars in the place of the missing text). One of the improvements you will probably consider for this application is to continue printing tall cells on the following page. To test the application I've created "fake" subitems with very long strings, to see how the code handles unusually tall cells. If a certain column contains long strings, you should give more space to this column. Printing in landscape mode will also minimize the problem of trimming very long items at the bottom of the page.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap