# VBForums CodeBank > CodeBank - Visual Basic .NET >  vb.net--How to use the printer

## Andy

This is a snippiet of code I struggled for weeks to figure out.  I hope this helps someone else out:



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

----------


## Andy

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)

----------


## Pirate

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  :Wink:

----------


## Andy

ah thanks!This is actually one of the first posts I made here so please forgive the newby-ness  :Big Grin:

----------


## taxes

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.

----------


## fuzzybunny

Andy,

Thank you for posting this, I have been hunting via internet for a few days for this  :Smilie: 

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

----------

