Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Expand Your VB6 Printing Repertoire—Part III : Page 4

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Helper Functions
The following code shows the two helper functions: the BreakItemText and GetNextWord functions. The BreakItemText function attempts to fit as many words into the specified width as possible, taking into consideration the current font. If needed, it creates multiple text lines separated by a vbCrLf character. To extract individual words from a string, it calls the GetNextWord function. The GetNextWorduses the space as the only valid word separator. Multiple spaces are not skipped; they're appended to the word they follow.

Private Function BreakItemText(ByVal item As String, _
                               ByVal width As Single) As String
    If PRN.TextWidth(item) < width Then
        BreakItemText = item
        Dim iChar As Integer: iChar = 1
        Dim newitem As String: newitem = ""
        Dim moreWords As Boolean: moreWords = True
        Dim nextWord As String
        While moreWords
            nextWord = GetNextWord(item, iChar)
            iChar = iChar + Len(nextWord)
            If PRN.TextWidth(newitem & nextWord) < width Then
                newitem = newitem & nextWord
                newitem = newitem & vbCrLf & nextWord
            End If
            BreakItemText = newitem
            If iChar > Len(item) Then
                moreWords = False
            End If
    End If
End Function

Private Function GetNextWord(ByVal str As String, _
                             ByVal pos As Integer)
    Dim nextWord As String
    While pos <= Len(str) And Mid(str, pos, 1) <> " "
        nextWord = nextWord & Mid(str, pos, 1)
        pos = pos + 1
    While pos <= Len(str) And Mid(str, pos, 1) = " "
        nextWord = nextWord & Mid(str, pos, 1)
        pos = pos + 1
    GetNextWord = nextWord
End Function

There are a few more procedures I'm not showing here, but you will find their implementation in the sample project. The DrawVerticalLines subroutine draws the vertical lines between columns and it's called after printing the last row on the page, because it needs to know the coordinates of the last row. The vertical lines extend from the top horizontal line to the last one that appears below the last row. The horizontal lines are printed from within the PrintSubItems subroutine after each row. The PrintDatePageNo subroutine prints the report's title, date, and page number at the top of the page. This is probably one of the first segments of the project you'll want to change. The PrintListViewHeaders subroutine prints the headers of the columns on each page. Its code is similar to the code of the PrintSubItems subroutine, in the sense that it takes into consideration the alignment of each column. The header titles, however, are not broken into multiple text lines (the code assumes that they will fit on a single line). Finally, the ShowMarginssubroutine draws on the preview form a white rectangle that represents the actual page (the preview form's background color is dark gray and the page area is white).

Open the PRNClass project and check it out. The code is adequately commented and you should be able to adjust it fairly easily. To reuse the LVPrint class, compile it to generate the LVPrint DLL and referenced it from within any project that uses the ListViewcontrol to display tabular data.

Evangelos Petroutsos is a long time VB developer. When he's not writing code, he writes programming books and articles. His most recent title is Mastering Visual Basic .NET, published by Sybex. Reach him by e-mail here.
Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date