Fog Creek Software
Discussion Board

Using PrintPreviewDialog and PrintDocument control

Can anyone point me to any tutorials on how to use the PrintPreviewDialog and PrintDocument controls in VB.NET?

VB.NET newbie
Tuesday, November 11, 2003

PrintDocument - An object representing the document to be printed.  You basically "install" a procedure to print onto the "Document"

1. Start a new VB.Net project
2. Draw a text box on the form
3. Draw a button on the form
4. Put the line - "Imports System.Drawing.Printing" at the top of the code file.
5. In the Button1_Click Event put the following code:

  'Instantiate a new PrintDocument object
  Dim PrintDoc as New PrintDocument()
  'Tell the PrintDocument object what procedure it should use to 'write on the page'
  AddHandler PrintDoc.PrintPage, AddressOf Me.PrintText
  PrintDoc.Print() 'Call the Print Procedure
Catch ex as Exception
  MessageBox.Show("Print Error - ", ex.ToString())
End Try

6. Create the Procedure that actually does the work.  Put this in the General Declarations Area.

Private Sub PrintText(byval sender as object, byval ev as PrintPageEventArgs)

  ev.Graphics.DrawString(TextBox1.Text, New Font("Arial", 11, FontStyle.Regular), Brushes.Black, 120, 120)

  ev.HasMorePages = False

End Sub

7. Run the Project.

Low Rider
Tuesday, November 11, 2003

*  Recent Topics

*  Fog Creek Home