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

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


n the past two 10-Minute Solutions, you've explored the basics of printing with VB6 and built a very simple text editor with print and preview capabilities. Now, learn how to print tabular data from a ListView control. The ListViewcontrol is very handy for displaying tabular data—though because it doesn't allow you to edit the data, you need a tool for printing its contents.

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 ListViewcontrol 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 ListViewcontrol. 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 ListViewcontrol 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 ListViewcontrol 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.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap