Click to See Complete Forum and Search --> : PrintDocument help please


mharley
November 23rd, 2004, 03:38 PM
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:


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.

DSJ
November 23rd, 2004, 03:46 PM
You need to add code to the objPrintDocuments.PrintPage event to do the printing.

mharley
November 23rd, 2004, 04:01 PM
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?

DSJ
November 24th, 2004, 08:18 AM
Yes, you'll need to use the e.graphics objects methods to draw text, lines, whatever you want printed.

mharley
November 25th, 2004, 12:27 PM
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)?

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?

mharley
November 25th, 2004, 12:43 PM
I finally got it figured out. I had forgotten the AddHandler line. Thanks for all your help.