Results 1 to 3 of 3

Thread: Printing from Textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2004
    Location
    LA
    Posts
    57

    Question Printing from Textbox

    Hello,

    I try to search multiple forums for good printing example on how to print from Textbox.

    Does any body have good printing example that utilizes the use of Pages Setup, Print Preview and ability to printing multiple pages.

    I really need this please help me.


  2. #2
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316
    This code creates a temp file and then sends it to the default printer. To make it print the contents of a text box you could just loop through each line using the writeline statement in each loop. It would automatically print on mulitple pages if needed.

    For a print preview maybe you could copy the contents of the textbox to a textbox on another form that is anchored to all four sides. Use the keypress event to handel any keys pressed so it doesn't behave like a text box. For previewing multiple pages you could figure out how many lines can print on each page and then insert a ----< page break >---- every nth line.

    You could add a couple buttons across the top of the print preview form to bring up a printer dialog for changing default printers and sending the code below to the printer. It sounds like a silly way to go about it but I bet it would work.

    Good luck.

    Code:
    Dim objPrint As New PrintDoc
    
    FileOpen(1, "TEMPFILE", OpenMode.Output) ' Open file for output.
    
    WriteLine(1, "PRINT ME!")
    
    FileClose(1)
    
    objPrint.PrintFile("TEMPFILE")
    Last edited by scuzymoto; Apr 8th, 2004 at 05:01 PM.
    SCUZ

  3. #3
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316
    After providing this code to someone else it became evident that the printdoc class was a custom class that without the code below would be useless, sorry...

    Code:
    code:--------------------------------------------------------------------------------
    Imports System
    Imports System.Drawing
    Imports System.Drawing.Printing
    Imports System.IO
    
    Public Class PrintDoc
    
        Private printFont As Font
        Private streamToPrint As StreamReader
    
        'Event fired when the user presses the print button
        Public Sub PrintFile(ByVal strFileToPrint As String)
    
            Try
                streamToPrint = New StreamReader(strFileToPrint)
                Try
                    printFont = New Font("Lucida Console", 9)
                    Dim pd As PrintDocument = New PrintDocument() 'Assumes the default printer
                    AddHandler pd.PrintPage, New System.Drawing.Printing.PrintPageEventHandler(AddressOf Me.pd_PrintPage)
                    pd.Print()
                Finally
                    streamToPrint.Close()
                End Try
    
            Catch ex As Exception
                MessageBox.Show("An error occurred printing the file - " + ex.Message)
            End Try
    
        End Sub
    
        'Event fired for each page to print
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
    
            Dim lpp As Single = 0
            Dim yPos As Single = 0
            Dim count As Integer = 0
            Dim leftMargin As Single = 30
            Dim topMargin As Single = 60
            Dim line As String
    
            'Work out the number of lines per page
            'Use the MarginBounds on the event to do this
            lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
    
            'Now iterate over the file printing out each line
            'NOTE WELL: This assumes that a single line is not wider than the page width
            'Check count first so that we don't read line that we won't print
            line = streamToPrint.ReadLine()
            While (count < lpp And line <> Nothing)
    
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics))
    
                ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
    
                count = count + 1
    
                If (count < lpp) Then
                    line = streamToPrint.ReadLine()
                End If
    
            End While
    
            'If we have more lines then print another page
            If (line <> Nothing) Then
                ev.HasMorePages = True
            Else
                ev.HasMorePages = False
            End If
    
        End Sub
    
    End Class
    SCUZ

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