Results 1 to 2 of 2

Thread: print to printer from a vb.net app

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    print to printer from a vb.net app

    how do I print a textbox and a listbox to my printer from a vb.net app?

  2. #2
    Member
    Join Date
    Dec 2002
    Location
    NY, USA
    Posts
    52

    Re: print to printer from a vb.net app

    Originally posted by yulyos
    how do I print a textbox and a listbox to my printer from a vb.net app?
    Code:
    'it does not matter what size of text box. It will create the paper size printout
    'Add PrintPreviewDialog, PrintDocument and PageSetup
    
    Public Class PrintTextForm
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    #End Region
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim txtFont As New Font("Arial", 10)
            Dim LMargin As Integer = PrintDocument1.DefaultPageSettings.Margins.Left
            Dim TMargin As Integer = PrintDocument1.DefaultPageSettings.Margins.Top
            Dim txtH As Integer = _
               PrintDocument1.DefaultPageSettings.PaperSize.Height - _
               PrintDocument1.DefaultPageSettings.Margins.Top - _
               PrintDocument1.DefaultPageSettings.Margins.Bottom
            Dim txtW As Integer = _
               PrintDocument1.DefaultPageSettings.PaperSize.Width - _
               PrintDocument1.DefaultPageSettings.Margins.Left - _
               PrintDocument1.DefaultPageSettings.Margins.Right
            Dim linesPerPage As Integer = _
               e.MarginBounds.Height / txtFont.GetHeight(e.Graphics)
            Dim R As New RectangleF(LMargin, TMargin, txtW, txtH)
            Static line As String
            Dim word As String
            Dim cols, lines As Integer
            word = GetNextWord()
            While word <> "" And lines < linesPerPage
                line = line & word
                word = GetNextWord()
                e.Graphics.MeasureString(line & word, txtFont, New SizeF(txtW, txtH), _
                                         New StringFormat(), cols, lines)
            End While
            If word = "" And Trim(line) <> "" Then
                e.Graphics.DrawString(line, txtFont, Brushes.Black, R, _
                                      New StringFormat())
                e.HasMorePages = False
                Exit Sub
            End If
            e.Graphics.DrawString(line, txtFont, Brushes.Black, R, New StringFormat())
            e.HasMorePages = True
            line = word
        End Sub
    
        Function GetNextWord(Optional ByVal reset As Boolean = False) As String
            Static currPos As Integer
            Dim word As String
    
            If reset Then currPos = 0
            If currPos >= TextBox1.Text.Length Then Return ""
            While Not System.Char.IsLetterOrDigit(TextBox1.Text.Chars(currPos))
                word = word & TextBox1.Text.Chars(currPos)
                currPos = currPos + 1
                If currPos >= TextBox1.Text.Length Then Return word
            End While
            While Not (System.Char.IsWhiteSpace(TextBox1.Text.Chars(currPos)))
                word = word & TextBox1.Text.Chars(currPos)
                currPos = currPos + 1
                If currPos >= TextBox1.Text.Length Then Return word
            End While
            Return word
        End Function
    
        Dim textToPrint As String
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            textToPrint = TextBox1.Text
    
            PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
            If PageSetupDialog1.ShowDialog() = DialogResult.OK Then
                PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
            End If
            Try
                PrintPreviewDialog1.Document = PrintDocument1
                PrintPreviewDialog1.ShowDialog()
            Catch exc As Exception
                MsgBox("Print operation failed " & vbCrLf & exc.Message)
            End Try
        End Sub
    
    End Class
    Iouri Boutchkine

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