|
-
November 23rd, 2004, 04:38 PM
#1
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.
-
November 23rd, 2004, 04:46 PM
#2
Re: PrintDocument help please
You need to add code to the objPrintDocuments.PrintPage event to do the printing.
-
November 23rd, 2004, 05:01 PM
#3
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.
-
November 24th, 2004, 09:18 AM
#4
Re: PrintDocument help please
Yes, you'll need to use the e.graphics objects methods to draw text, lines, whatever you want printed.
-
November 25th, 2004, 01:27 PM
#5
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.
-
November 25th, 2004, 01:43 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|