KnowDotNet

Why Migrate to .NET? Printing is different, but much more powerful and flexible than VB6.

CPrintString - A Complete Class for Printing a Report From a String.

by Les Smith

Why Migrate to .NET?  Printing is completely different, but much more powerful and flexible than VB6.  Once you get over the obvious (printing is different in .NET), you will see that it is much more powerful than you have been used to in VB6.

In this article, I will present a complete class for printing a report from a string created in memory.  Although, this is a base class, which you can enhance, it is a powerful class, in that it automatically creates a Title, SubTitle, Footer, and from 1-4 column headers.  It also provides several keywords that allow you to force page breaks, underline print lines, change headers on page breaks, etc.  You will find a description of these keywords in the PrintDoc event of the class.

Two Methods are exposed by this class.  One is for Previewing your report and the other is for printing directly to the printer.  The calls to both are made passing the same set of parameters, which you can see by viewing the respective methods.  Once called, these methods will instantiate a PrintDocument object, passing the AddressOf the PrintDoc event handler.  Calling the print or preview methods will cause the PrintDocument object to fire the PrintPage event each time it wants a page of print.  In VB6, printing was done lineraly through the Printer Object.  In .NET, there is no Printer Object.  Printing is done through a CallBack methodology.  The user initiates the printing and then the PrintDocument object calls the PrintPage event to get a page of data to print.

Not to worry, this class will handle all of the interfacing with the NET PrintDocument object for you.  This is a large class, and it is documented with comments in the code, so I will not attempt to describe it further.  I will show you a simple call to it.  This call assumes that the complete report has been built as a string, delimited by CRLFs, and you simply call the class to handle the printing.  You can change this paradigm by putting code in the PrintPage event to retrieve your data from database, etc.

Figure 1 - Calling the Print Method.

       ' Create an instance of the print class
      ' ps is a StringBuilder object containing the report string
      ' to be printed

      Dim oCP As New CPrintString()
      
' set the title fontsize, make it bold, italic
      oCP.TitleFontSize = 14
      oCP.TitleFontStyle = "BI"

      
Dim colHdr As String = ""
      colHdr &= "Equip ID".PadRight(10)
      colHdr &= "Equip Name".PadRight(27)
      colHdr &= "Rent Date".PadRight(11)
      colHdr &= "Returned".PadRight(11)
      colHdr &= "Per".PadRight(4)
      colHdr &= "No.".PadRight(4)
      colHdr &= "Days".PadRight(4)
      colHdr &= "Revenue".PadLeft(12)

      
Dim title As String = ReportName
      
Dim subTitle As String = _
            "Equipment Usage Report: " & Format(
Me.StDate, "M/d/yyyy") & _
            " To " & Format(
Me.EndDate, "M/d/yyyy")
      
' here you would insert code to format your report lines
      ' in a StringBuilder object (ps)

      ' For i As Integer = 0 To datatable.rows.count-1
      '    format print lines from database data, e.g....
      ' Next i

      ' after the complete report is in the stringbuilder
      ' call a print or preview method...

      
' once the call is made to either method, your report
      ' will be printed automatically, with no further work on your part

      
If Me.Preview Then
         oCP.PrintPreview(96, ps.ToString, title, subTitle, _
            ColHdr1:=colHdr)
      
Else
         oCP.StartPrint(96, ps.ToString, title, subTitle, _
            ColHdr1:=colHdr)
      
End If

You can download the code for the CPrintString Class by clicking
here.