Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, March 14, 2013 12:57 AM
Where do I specify my file name in the following code:
' Declare the PrintDocument object.
Private WithEvents docToPrint As New Printing.PrintDocument
' This method will set properties on the PrintDialog object and
' then display the dialog.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Allow the user to choose the page range he or she would
' like to print.
PrintDialog1.AllowSomePages =
True
' Show the help button.
PrintDialog1.ShowHelp =
True
' Set the Document property to the PrintDocument for
' which the PrintPage Event has been handled. To display the
' dialog, either this property or the PrinterSettings property
' must be set
PrintDialog1.Document = docToPrint
Dim result As DialogResult = PrintDialog1.ShowDialog()
' If the result is OK then print the document.
If (result = DialogResult.OK) Then
docToPrint.Print()
End If
End Sub
' The PrintDialog will print the document
' by handling the document's PrintPage event.
Private Sub document_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles docToPrint.PrintPage
' Insert code to render the page here.
' This code will be called when the control is drawn.
' The following code will render a simple
' message on the printed document.
Dim text As String = "In document_PrintPage method."
Dim printFont As New System.Drawing.Font _
(
"Arial", 35, System.Drawing.FontStyle.Regular)
' Draw the content.
e.Graphics.DrawString(text, printFont, _
System.Drawing.
Brushes.Black, 10, 10)
End Sub
ecb
All replies (5)
Thursday, March 14, 2013 3:46 AM âś…Answered
I created a file in the note pad .txt and I need some understanding of where do I go from here. Should I use PrintDocument, Printform, or Printdialog? I do not wish for the user to get involved on selecting a file. Which one of the above should I focused my attention? Any sample code would be appreciated that would give me a start.
Your user is going to get involved in selecting a file if you want to print a file.
If you want to print some text that is part of your application, then the user does not need to get involved with a file.
So the question is: how do you intend to get the file contents into your application? Will the filename always be exactly the same, and will the file always be stored at the same location on the drive?
If the answer is Yes, then you can access the file directly as part of the printing process. Then you can use the template from here:
http://vbdotnetblog.wordpress.com/examples/simple-text-printing/
if you replace this:
Private Sub OnGetTextFile(ByVal sender As [Object], ByVal e As EventArgs)
Dim TD As OpenFileDialog = New OpenFileDialog
TD.Filter = "Text Files|*.txt|All Files|*.*"
If TD.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
PrintDocument.Text = System.IO.File.ReadAllText(TD.FileName)
End If
End Sub
with this:
Private Sub OnGetTextFile(ByVal sender As [Object], ByVal e As EventArgs)
PrintDocument.Text = System.IO.File.ReadAllText("<the path and name of the text file to be printed>")
End Sub
Thursday, March 14, 2013 1:42 AM
the PrintDialog just allows you to ask your user what they want to print.
you have to write the code which will actually do the printing in the PrintDocument_PrintPage event
thanks for any help
Thursday, March 14, 2013 3:32 AM
Paul, this is my first time attempting to print something in VB and I do not know where to start. I created a file in the note pad .txt and I need some understanding of where do I go from here. Should I use PrintDocument, Printform, or Printdialog? I do not wish for the user to get involved on selecting a file. Which one of the above should I focused my attention? Any sample code would be appreciated that would give me a start.
ecb
Thursday, March 14, 2013 7:25 PM
Acamar, thanks for the information. Yes, I will create the file within a VB application and the file name and location will always be the same. I will use the code you sent. Thanks very much.
ecb
Thursday, March 14, 2013 9:28 PM
Acamar, I am new at this so please excuse. I followed your instructions but I am doing something wrong. I created a new form and created button1 that I click to get to the code you gave me. I Change the code pointing to my text file and I put a trace on the button1 click. From the button1 click it goes to Print Dialog and when I leave the print dialog box it execute the Sub Document_PrintPage and then it print one line as follows: IN DOCUMENT_PRINTPAGE MEHTOD.
It never get to the code where I inserted my filename. What am I doing incorrect. Remember I am new a this so please excuse.
Ed.
ecb