# VBForums CodeBank > CodeBank - Visual Basic .NET >  Printing example, short, simple & easy.

## noahssite

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

----------


## noahssite

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.

----------


## noahssite

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.

----------


## Merrion

For info, there is code that does the printing pictures, different fonts and so on in this codebank item...

----------


## 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.

----------


## noahssite

> 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.



```
Dim Print As New myPrinter
```

----------


## veebienewbie

Where do I get it from?

----------


## noahssite

> 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.

----------

