The ListView Control
Figure 1 shows the test form of this article's sample project, which contains two
ListView controls displaying different sets of data, and a preview form for the customer data. The Print and Preview buttons create an instance of the
LVPrint class, set a few properties, and then call the
PrintList method to preview and print the data. The PrintList method picks up all the information from the corresponding
ListView control and generates the printout. The following is the core code behind the Preview buttons:
'Printing the top ListView
Private Sub bttnPreviewCustomers_Click()
Dim LVPRN As New PRNClass.LVPrint
Set LVPRN.LV = ListView1
LVPRN.ReportTitle = "Northwind Customers"
If Not LVPRN.PrintList(True) Then
MsgBox "Preview failed!"
End If
End Sub
'Printing the bottem ListView
Private Sub bttnPreviewInvoice_Click()
Dim LVPRN As New PRNClass.LVPrint
Set LVPRN.LV = ListView2
LVPRN.ReportTitle = "Simple Invoice Printout Demo"
If Not LVPRN.PrintList(True) Then
MsgBox "Preview failed!"
End If
End Sub
 | |
| Figure 1. The test form for this article's sample project contains two ListView controls displaying different sets of data and a preview form for the customer data. |
The argument of the PrintList method determines whether the data will be previewed (True) or printed (False). The LVPrint class can handle any ListView control and you don't really need to understand how it works; just use it to add print and preview capabilities to any form that displays data on the ListView control. To make the most of this code, review it so you are able to edit it to accommodate your application's specific requirements.
The basic code for printing the items of a ListView control is straightforward, but the details introduce a few interesting challenges. It would be fairly easy to print the items on one line per cell, which is how the ListView control displays its data in Report mode (View property); if an item's text is too long to fit on a single line, only part of it is visible. A decent printout, however, requires that long strings are broken into multiple lines of the text, which are printed in a tall cell. All cells on the same row must have the same height, which is determined by the height of the tallest cell. We'll look closer at the code for wrapping each cell's text shortly. The code also picks the alignment information from the ListView control being printed and uses it to align the text in every cell.
In addition to breaking long cells into multiple lines of text, the LVPrint class maintains the ratio of the control's columns. The ratio of each column's width to the total width of the control is the same on both the preview form and the printed page. A column that takes 15 percent of the ListView control's width will take 15 percent of the printable area on the page. This means that users can resize the columns of the ListView control to affect the appearance of the printout. Printing the report sideways will give you room to fit more data on each line than across a typical monitor.