-
Sep 2nd, 2008, 06:39 PM
#1
Thread Starter
Frenzied Member
Printing example, short, simple & easy.
After looking on the internet i have found some code for printing the text of a richtextbox. I added comments and added a few things...
Commented Code:
vb.net Code:
'NOTE * // = opening comments and \\ = closing comments...
Public Class Form1
'Declares public variables...
Public printer As String
Public copies As Integer
Private Sub cmdPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdPrint.Click
Dim Print As New myPrinter 'Declares Print as a new myPrinter class.
Dim myprintdialog As New PrintDialog 'Creates Print Dialog.
With myprintdialog
If .ShowDialog = Windows.Forms.DialogResult.OK Then
'//------
printer = .PrinterSettings.PrinterName 'Sets variable printer to selected
'printers name.
'\\------
'//------
copies = .PrinterSettings.Copies 'Sets ammount of copies to number specified
' in dialog.
'\\------
'//------
Print.prt(rtbText.Text.Trim) 'calls the prt sub in
'the myPrinter class with text (a string) set to the text in the rtb.
'Basicly starts the printing process...
'\\------
End If
End With
End Sub
End Class
'Print Class
Public Class myPrinter
Friend TextToBePrinted As String 'Declares TextToBePrinted as a string.
'Below is the sub that prints the text to the printer.
Public Sub prt(ByVal text As String)
TextToBePrinted = text
Dim prn As New Printing.PrintDocument
Using (prn)
prn.PrinterSettings.PrinterName = Form1.printer
prn.PrinterSettings.Copies = Form1.copies
'// Adds a handler for PrintDocument.PrintPage
'(the sub PrintPageHandler)
AddHandler prn.PrintPage, _
AddressOf Me.PrintPageHandler
'\\
prn.Print() 'Prints.
'// Removes the handler for PrintDocument.PrintPage
'(the sub PrintPageHandler)
RemoveHandler prn.PrintPage, _
AddressOf Me.PrintPageHandler
'\\
End Using
End Sub
'Below is code that sets the fonts etc...
Private Sub PrintPageHandler(ByVal sender As Object, _
ByVal args As Printing.PrintPageEventArgs)
Dim myFont As New Font("Microsoft San Serif", 10)
args.Graphics.DrawString(TextToBePrinted, _
New Font(myFont, FontStyle.Regular), _
Brushes.Black, 50, 50)
End Sub
End Class
Not Commented Code:
vb.net Code:
Public Class Form1
Public printer As String
Public copies As Integer
Private Sub cmdPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdPrint.Click
Dim Print As New myPrinter
Dim myprintdialog As New PrintDialog
With myprintdialog
If .ShowDialog = Windows.Forms.DialogResult.OK Then
printer = .PrinterSettings.PrinterName
copies = .PrinterSettings.Copies
Print.prt(rtbText.Text.Trim)
End If
End With
End Sub
End Class
Public Class myPrinter
Friend TextToBePrinted As String
Public Sub prt(ByVal text As String)
TextToBePrinted = text
Dim prn As New Printing.PrintDocument
Using (prn)
prn.PrinterSettings.PrinterName = Form1.printer
prn.PrinterSettings.Copies = Form1.copies
AddHandler prn.PrintPage, _
AddressOf Me.PrintPageHandler
prn.Print()
RemoveHandler prn.PrintPage, _
AddressOf Me.PrintPageHandler
End Using
End Sub
Private Sub PrintPageHandler(ByVal sender As Object, _
ByVal args As Printing.PrintPageEventArgs)
Dim myFont As New Font("Microsoft San Serif", 10)
args.Graphics.DrawString(TextToBePrinted, _
New Font(myFont, FontStyle.Regular), _
Brushes.Black, 50, 50)
End Sub
End Class
-
Oct 14th, 2008, 09:51 PM
#2
Thread Starter
Frenzied Member
Re: Printing example, short, simple & easy.
If anyone has any other printing code and you think it is better then the code i supplied please reply with the code or a link.
-
Dec 23rd, 2008, 11:06 AM
#3
Thread Starter
Frenzied Member
Re: Printing example, short, simple & easy.
I will update my code soon and make it more flexible, for printing pictures, different varies of fonts in one page etc...
Right now it is a very simple example.
-
Feb 20th, 2009, 09:17 AM
#4
Re: Printing example, short, simple & easy.
For info, there is code that does the printing pictures, different fonts and so on in this codebank item...
-
Jul 23rd, 2009, 11:57 AM
#5
Lively Member
Re: Printing example, short, simple & easy.
I tried using your code, it throws an error at:
The error says:Type 'myPrinter' is not defined.
VB Code:
Dim Print As myPrinter 'Declares Print as a new myPrinter class.
Last edited by veebienewbie; Jul 23rd, 2009 at 12:23 PM.
-
Jul 23rd, 2009, 02:05 PM
#6
Thread Starter
Frenzied Member
Re: Printing example, short, simple & easy.
Originally Posted by veebienewbie
I tried using your code, it throws an error at:
The error says:Type 'myPrinter' is not defined.
VB Code:
Dim Print As myPrinter 'Declares Print as a new myPrinter class.
You should use the New keyword.
Code:
Dim Print As New myPrinter
-
Jul 23rd, 2009, 02:07 PM
#7
Lively Member
Re: Printing example, short, simple & easy.
-
Jul 23rd, 2009, 02:10 PM
#8
Thread Starter
Frenzied Member
Re: Printing example, short, simple & easy.
Originally Posted by veebienewbie
Where do I get it from?
I made a mistake, see post #6 again, I made an edit.
Anyway the class myPrinter is supplied in the example.
-
Jan 16th, 2024, 05:11 AM
#9
New Member
Re: Printing example, short, simple & easy.
Thread starter, Mate you wrote the simple and easy printing code in 2008... 16 years on I do thank you. hope you are travelling well.
Printing example, short, simple & easy. the highest praise in science is to say your work is eloquent. Your work my friend is eloquent.
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
|