Results 1 to 6 of 6

Thread: vb.net--How to use the printer

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    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

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    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)

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If you would allow me to change your code , this sub should b like this :

    VB Code:
    1. Private Sub print()
    2.  
    3.         Try
    4.             stream = New System.IO.StreamReader(strFileName)
    5.                 Dim doctoprint As New PrintDocument
    6.                 AddHandler doctoprint.PrintPage, AddressOf document_PrintPage
    7.                 doctoprint.Print()
    8.         Catch ex As Exception
    9.             MessageBox.Show(ex.Message)
    10.  
    11.             Finally
    12.                 stream.Close()
    13.         End Try
    14.  
    15.     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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    ah thanks!This is actually one of the first posts I made here so please forgive the newby-ness

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  6. #6
    New Member
    Join Date
    Nov 2006
    Location
    14094
    Posts
    1

    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
  •  



Click Here to Expand Forum to Full Width