PrintDialog Control in Visual Basic .NET

In your VB.NET application, you can print the document using PrintDialog. But PrintDialog does not do any printing but it opens the PrintDialog and you can select the printer and page orientation etc.

You can see the PrintDialog in the following figure

I am going to print the text in the RichTextBox controller. Following form shows the RichTextBox and Button

To print a document you need to have PrintDocument class. You can get this class by importing System.Drawing.Printing. You have to set the Document property of PrintDialog to PrintDocument object

Also you have to write some code in PrintPage event handler of the PrintDocument class

This is the complete code to print the text in RichTextBox control

Code explanation

  • Line 1 : Importing the System.Drawing.Printing for PrintDocument class
  • Line 4 : Creating PrintDocument instance
  • Line 8 : Assign PrintDocument instance to Document property of the PrintDialog
  • Line 11: Executing the print() of the PrintDocument
  • Line 17: Passing arguments to DrawString() which will be used to print the string

This is the very basic code to print regular text. But printing is very complex and you have lot of setting to study

Leave a Reply

Your email address will not be published. Required fields are marked *