PrintDialog help

Getox

Centurion
Joined
Jul 8, 2004
Messages
122
Hi, me once again asking about the Dialogs

i want to print the text in "TextBox"
so i use "Me.PrintDialog.ShowDialog()" and it crashes to app
help would be really great.

Error: System.ArgumentException: PrintDialog needs a PrinterSettings object to display. Please set PrintDialog.Document (preferred) or PrintDialog.PrinterSettings.
 
Writing print handlers can be tricky when you haven't done many, but really fun after you get it down. You'll need two subs to print, one for the print dialog and/or print preview dialog and another sub to print the document with PrintDocument1

Here's an example of a simple print document sub utilizing PrintDialog.

Visual Basic:
Private Sub Print()
    Dim pDialog As New PrintDialog()
    pDialog.Document = PrintDocument1
    If pDialog.ShowDialog = DialogResult.OK Then
          PrintDocument1.Print()
    End If
End Sub

Visual Basic:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
   Static intCurrentChar As Int32
   Dim font As New Font("Arial", 10)
   Dim Text2Print As String
   Text2Print = txtMain.Text
        
   Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
   With PrintDocument1.DefaultPageSettings
          intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
          intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
          marginLeft = .Margins.Left ' X coordinate
          marginTop = .Margins.Top ' Y coordinate
   End With

   If PrintDocument1.DefaultPageSettings.Landscape Then
      Dim intTemp As Int32
      intTemp = intPrintAreaHeight
      intPrintAreaHeight = intPrintAreaWidth
      intPrintAreaWidth = intTemp
   End If

   Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
   Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
   Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
   Dim intLinesFilled, intCharsFitted As Int32
   e.Graphics.MeasureString(Mid(Text2Print, intCurrentChar + 1), font, _
                    New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
                    intCharsFitted, intLinesFilled)

    e.Graphics.DrawString(Mid(Text2Print, intCurrentChar + 1), font, _
            Brushes.Black, rectPrintingArea, fmt)

    intCurrentChar += intCharsFitted
   If intCurrentChar < Text2Print.Length Then
       e.HasMorePages = True
   Else
      e.HasMorePages = False
      intCurrentChar = 0
  End If
End Sub

This is only one example, and there are so many different ways to print something that maybe you should check out MSDN's simple printing example in their 101 examples or even a programming book.
 
Back
Top