View Single Post
  #4 (permalink)  
Old June 30th, 2008, 04:49 AM
Thearon's Avatar
Thearon Thearon is offline
Wrox Author
 
Join Date: Dec 2003
Location: Fuquay Varina, NC, USA.
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

OK, here it is. This example assumes you have text in a textbox that you want to print.

Add a PrintDialog control to your form. Then add the following class level declarations:

    Private strPrintRecord As String
    Private WithEvents DialogsPrintDocument As PrintDocument

Now add this procedure:

    Private Sub DialogsPrintDocument_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
        Handles DialogsPrintDocument.PrintPage

        'Declare variables
        Dim intCharactersToPrint As Integer
        Dim intLinesPerPage As Integer
        Dim strPrintData As String
        Dim objStringFormat As New StringFormat
        Dim objPrintFont As New Font("Arial", 10)
        Dim objPageBoundaries As RectangleF
        Dim objPrintArea As SizeF

        'Get the page boundaries
        objPageBoundaries = New RectangleF(e.MarginBounds.Left, _
            e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)

        'Get the print area based on page margins and font used
        objPrintArea = New SizeF(e.MarginBounds.Width, _
            e.MarginBounds.Height - objPrintFont.GetHeight(e.Graphics))

        'Break in between words on a line
        objStringFormat.Trimming = StringTrimming.Word

        'Get the number of characters to print
        e.Graphics.MeasureString(strPrintRecord, objPrintFont, objPrintArea, _
            objStringFormat, intCharactersToPrint, intLinesPerPage)

        'Get the print data from the print record
        strPrintData = strPrintRecord.Substring(0, intCharactersToPrint)

        'Print the page
        e.Graphics.DrawString(strPrintData, objPrintFont, Brushes.Black, _
            objPageBoundaries, objStringFormat)

        'If more lines exist, print another page
        If intCharactersToPrint < strPrintRecord.Length Then
            'Remove printed text from print record
            strPrintRecord = strPrintRecord.Remove(0, intCharactersToPrint)
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub

Finally double click on the Print button on your form and add this code:

    Private Sub btnPrint_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnPrint.Click

        'Instantiate a new instance of the PrintDocument
        DialogsPrintDocument = New PrintDocument

        'Set the PrintDialog properties
        With PrintDialog1
            .AllowCurrentPage = False
            .AllowPrintToFile = False
            .AllowSelection = False
            .AllowSomePages = False
            .Document = DialogsPrintDocument
            .PrinterSettings.DefaultPageSettings.Margins.Top = 25
            .PrinterSettings.DefaultPageSettings.Margins.Botto m = 25
            .PrinterSettings.DefaultPageSettings.Margins.Left = 25
            .PrinterSettings.DefaultPageSettings.Margins.Right = 25
        End With

        If PrintDialog1.ShowDialog = DialogResult.OK Then
            'Set the selected printer settings in the PrintDocument
            DialogsPrintDocument.PrinterSettings = _
                PrintDialog1.PrinterSettings

            'Get the print data
            strPrintRecord = txtFile.Text

            'Invoke the Print method on the PrintDocument
            DialogsPrintDocument.Print()
        End If
    End Sub


Thearon