CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2006
    Posts
    28

    Resolved Problems with PrintDocument

    I'm trying to print a few lines of text using PrintDocument

    I'm using VB.NET version 1

    the following line:

    Dim prn As New PrintDocument

    causes this error :

    Type 'printdocument' is not defined

    any ideas?

    H
    Last edited by happyme; November 12th, 2006 at 04:13 PM. Reason: resolved

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Problems with PrintDocument

    Sample prints the content of richTextBox

    Imports
    Code:
    Imports System.Drawing.Printing
    Imports System.IO
    Declarations
    Code:
        Dim printDialog1 As PrintDialog
        Dim WithEvents pDoc As PrintDocument
        Dim myReader As StringReader
        Dim line As String = Nothing
    Print Button
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            printDialog1 = New PrintDialog
            pDoc = New PrintDocument()
    
            printDialog1.Document = pDoc
    
            Dim strText As String = Me.richTextBox1.Text
            myReader = New StringReader(strText)
    
            If printDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then           
                pDoc.Print()
            End If
    
        End Sub
    Code:
        Private Sub mdoc_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pDoc.BeginPrint
            line = myReader.ReadLine
        End Sub
    Code:
        Private Sub mdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pDoc.PrintPage
    
            Dim yPosition As Single = 0 'initial position
            Dim count As Integer = 0    'line counter
    
            Dim leftMargin As Single = e.MarginBounds.Left
            Dim topMargin As Single = e.MarginBounds.Top
    
    
            Dim printFont As Font = Me.RichTextBox1.Font
            'get number of lines per page
            Dim linesPerPage As Single = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
    
            ' Iterate over the string using the StringReader, printing each line.
            While count < linesPerPage And Not (line Is Nothing)
                ' get next line position
                yPosition = (topMargin + (count * printFont.GetHeight(e.Graphics)))
                'draw line
                e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPosition, New StringFormat())
                count += 1
                'get next line
                line = myReader.ReadLine
            End While
    
            ' If there are more lines, print another page. 
            If Not (line Is Nothing) Then
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
    
        End Sub

  3. #3
    Join Date
    Oct 2006
    Posts
    28

    Resolved Re: Problems with PrintDocument

    Thank you very much for your time.

    this bit:

    Code:
    Imports System.Drawing.Printing
    Imports System.IO
    solved all my problems

    Thank you

    H
    I'm using .NET Framework 3.5

    I'm planning to be spontaneous tomorrow

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

Featured