CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2003
    Posts
    27

    PrintDocument help please

    I am trying to print the contents of a text box, and while I can get the program to activate the printer and feed paper, nothing gets printed on the paper. This is because I don't know how to send the contents of the text box to the printdocument. I'll post the code below:

    Code:
    Dim textToPrint As String = txtData.Text
    Dim textPrint As New PrintDialog()
    Dim objPrintDocument As PrintDocument = New PrintDocument()
    
    objPrintDocument.DocumentName = "Journal"
    textPrint.AllowPrintToFile = False
    textPrint.AllowSomePages = False
    textPrint.Document = objPrintDocument
    
    If textPrint.ShowDialog = DialogResult.OK Then
           objPrintDocument.PrinterSettings = textPrint.PrinterSettings
           objPrintDocument.Print()
    End If
    What am I missing? Any help is appreciated.

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: PrintDocument help please

    You need to add code to the objPrintDocuments.PrintPage event to do the printing.

  3. #3
    Join Date
    Jan 2003
    Posts
    27

    Re: PrintDocument help please

    What code do I need to include in the printpage event? I've set things such as the hasmorepages property, but that's it. Is this where I set the source for what should be printed?
    Last edited by mharley; November 23rd, 2004 at 06:18 PM.

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: PrintDocument help please

    Yes, you'll need to use the e.graphics objects methods to draw text, lines, whatever you want printed.

  5. #5
    Join Date
    Jan 2003
    Posts
    27

    Re: PrintDocument help please

    Ok, I still can't get it to work. Below is the code I'm using. Keep in mind that the text I wish to print is coming directly from a text box. Do I have to read it from the text box line by line (is this possible)?
    Code:
    Private Sub objPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs)
         Dim sngLinesPerPage As Single = 0
         Dim sngVerticalPosition As Single = 0
         Dim intLineCount As Integer = 0
         Dim sngLeftMargin As Single = e.MarginBounds.Left
         Dim sngTopMargin As Single = e.MarginBounds.Top
         Dim strLine As String
         Dim objPrintFont = New Font("Arial", 10)
         sngLinesPerPage = e.MarginBounds.Height / objPrintFont.getheight(e.Graphics)
         strLine = txtData.Text
         e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, sngLeftMargin,      
              sngVerticalPosition, New StringFormat())
         e.HasMorePages = False
    End Sub
    I can't even be sure that the sub is being called. When I add a breakpoint, it never gets hit. Am I missing something?
    Last edited by mharley; November 25th, 2004 at 01:35 PM.

  6. #6
    Join Date
    Jan 2003
    Posts
    27

    Re: PrintDocument help please

    I finally got it figured out. I had forgotten the AddHandler line. Thanks for all your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured