|
-
Dec 16th, 2003, 08:07 PM
#1
Thread Starter
Frenzied Member
vb.net--How to use the printer
This is a snippiet of code I struggled for weeks to figure out. I hope this helps someone else out:
Code:
Private Sub mnuPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrint.Click
PrintDialog1.AllowSomePages = True
PrintDialog1.ShowHelp = True
PrintDialog1.Document = docToPrint
Dim result As DialogResult = PrintDialog1.ShowDialog()
If (result = DialogResult.OK) Then
print()
End If
End Sub
Private Sub print()
Try
stream = New System.IO.StreamReader(strFileName)
Try
Dim doctoprint As New PrintDocument
AddHandler doctoprint.PrintPage, AddressOf document_PrintPage
doctoprint.Print()
Finally
stream.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub document_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docToPrint.PrintPage
Dim linesPerPage As Single
Dim yPos As Single
Dim count As Integer
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim printThis As String
' Calculate the number of lines per page
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
' go over the file, printing each line one at a time
While count < linesPerPage
printThis = stream.ReadLine()
If printThis Is Nothing Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(e.Graphics)
'e.Graphics.DrawString(printThis, printFont, System.Drawing.Brushes.Black, 10, 10)
e.Graphics.DrawString(printThis, printFont, Brushes.Black, leftMargin, yPos, New StringFormat)
count += 1
End While
' If more lines exist, print another page.
If Not (printThis Is Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
just change what you need to and it will cause the printer dialog box to open allowing the user to select the printer to use and all that good stuff!!
needed on form:
print dialog control
-
Dec 16th, 2003, 08:12 PM
#2
Thread Starter
Frenzied Member
oops...
you will also need the following imports:
Imports System.Drawing
Imports System.Drawing.Printing
and the following module level variables:
Dim stream As StreamReader
'variable for the printing font. I put this up here for easy alterations
Dim printFont As New System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Regular)
-
Aug 8th, 2004, 12:54 AM
#3
Sleep mode
If you would allow me to change your code , this sub should b like this :
VB Code:
Private Sub print()
Try
stream = New System.IO.StreamReader(strFileName)
Dim doctoprint As New PrintDocument
AddHandler doctoprint.PrintPage, AddressOf document_PrintPage
doctoprint.Print()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
stream.Close()
End Try
End Sub
There's no sense of using two Try statments here . You just surround your suspecious code with one block Try...Catch . If there's specific exception you think it would error out , then you can have multiple Catch statements that descending from the most specific to the most general one . Nice share , btw
Last edited by Pirate; Aug 8th, 2004 at 03:37 AM.
-
Aug 9th, 2004, 07:22 AM
#4
Thread Starter
Frenzied Member
ah thanks!This is actually one of the first posts I made here so please forgive the newby-ness
-
Oct 30th, 2004, 03:59 PM
#5
PowerPoster
I think you also need to use
Imports System.IO in order to use
Dim stream As StreamReader
and possibly
Imports System.
When you use
PrintDialog1.Document = docToPrint
is docToPrint a string to be passed to the Sub? Does it bear any relation to the doctoprint declared locally in the Sub Print()
Thanks in advance.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Nov 2nd, 2006, 10:14 AM
#6
New Member
Re: vb.net--How to use the printer
Andy,
Thank you for posting this, I have been hunting via internet for a few days for this 
However, when I copy/paste I'm getting errors on the following line.
Private Sub document_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles doctoprint.PrintPage
the last section "Handles doctoprint.printpage" says it requires a with events or something. The doctoprint has a squiggly blue underline.
Sorry for my .net n00bness, I'm a networking guy and this is just something I'm playing with to earn another competancy from MS. If you'd like my source it woulnd't be a problem.
Thank you,
Jon
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
|