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