|
|
Title | Print the values in a ListView control |
Keywords | ListView, print |
Categories | Controls, Graphics |
|
|
This example prints the data only in a report format and it sizes itself to display all of the values' text. It does not print icons or other ListView display formats.
When the user selects the Print command, the program calls subroutine PrintListView, passing it the ListView control. PrintListView loops through the column headers calculating their widths.
Then for each list item, the program compares the widths of the item and its subitems to the current column widths and makes the widths big enough to hold the items.
The program loops through the columns printing their headers. It uses the column widths it calculated to position the columns. Next the program loops through the items, printing each item and its subitems, again using the calculated column widths.
Finally the program draws lines around the whole thing and between the columns.
|
|
Private Sub mnuFilePrint_Click()
Printer.CurrentX = 1440
Printer.CurrentY = 1440
PrintListView ListView1
Printer.EndDoc
End Sub
Private Sub PrintListView(lvw As ListView)
Const MARGIN = 60
Const COL_MARGIN = 240
Dim ymin As Single
Dim ymax As Single
Dim xmin 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
xmin = Printer.CurrentX
ymin = Printer.CurrentY
' ******************
' Get column widths.
num_cols = lvw.ColumnHeaders.Count
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.
For i = 1 To num_cols
col_wid(i) = col_wid(i) + COL_MARGIN
Next i
' *************************
' Print the column headers.
Printer.CurrentY = ymin + MARGIN
Printer.CurrentX = xmin + MARGIN
X = xmin + MARGIN
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
xmax = X + MARGIN
Printer.Print
line_hgt = Printer.TextHeight("X")
Y = Printer.CurrentY + line_hgt / 2
Printer.Line (xmin, Y)-(xmax, Y)
Y = Y + line_hgt / 2
' Print the rows.
num_subitems = num_cols - 1
For Each list_item In lvw.ListItems
X = xmin + MARGIN
' Print the item.
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
Next list_item
ymax = Y
' Draw lines around it all.
Printer.Line (xmin, ymin)-(xmax, ymax), , B
X = xmin + MARGIN / 2
For i = 1 To num_cols - 1
X = X + col_wid(i)
Printer.Line (X, ymin)-(X, ymax)
Next i
End Sub
' Return as much text as will fit in this width.
Private Function FittedText(ByVal txt As String, ByVal wid _
As Single) As String
Do While Printer.TextWidth(txt) > wid
txt = Left$(txt, Len(txt) - 1)
Loop
FittedText = txt
End Function
|
|
Note that this program assumes there is room on the page to fit all of the data. If the ListView contains many items, you may need to split it across more than one page.
|
|
|
|
|
|