How to Create a Print Preview Control in Visual Basic

Creating a Windows Application and need to know how to create a print and print preview control? Follow these simple steps and you will easily be able to print and print preview documents in your application. Adjust the form to the size...

Part 1 of 3:

The Form

  1. Picture 1 of How to Create a Print Preview Control in Visual Basic
    Adjust the form to the size you require.
    1. It doesn't matter what size the form is, providing it can fit a large textbox and two buttons.
  2. Picture 2 of How to Create a Print Preview Control in Visual Basic
    Add a RichTextBox to your form.
    1. You can add a RichTextBox by double-clicking or dragging it onto the form.
    2. Adjust the size of the RichTextBox to your requirements.
  3. Picture 3 of How to Create a Print Preview Control in Visual Basic
    Add two Buttons to your form.
    1. Ideally, place the two Buttons near the RichTextBox.
    2. Again, you can add Buttons to your form by double-clicking or dragging onto the form.
  4. Picture 4 of How to Create a Print Preview Control in Visual Basic
    Name the Buttons "Print" and "Print Preview".
    1. You can change the text for the buttons in the properties box.
  5. Picture 5 of How to Create a Print Preview Control in Visual Basic
    Add the controls: "Print Document" and "PrintPreviewDialog" to your form.
    1. These will not be visible on the form as they perform in the background.
  6. Picture 6 of How to Create a Print Preview Control in Visual Basic
    Click on PrintPreviewDialog and change the "Document" to "PrintDocument1".
    1. You can change this in the properties box after clicking on PrintPreviewDialog.
Part 2 of 3:

The Code

  1. Picture 7 of How to Create a Print Preview Control in Visual Basic
    Double-click on your "Print Preview" button.
    1. This will bring up the page for coding.
    2. A Private Sub has already been created and awaiting code to tell it what to do when the Print Preview button is clicked.
    3. Add the following code into the Private Sub: PrintPreviewDialog1.ShowDialog()
  2. Picture 8 of How to Create a Print Preview Control in Visual Basic
    Go back to the form and double-click on your "Print" button.
    1. You will be taken back to the page for coding.
    2. In the Private Sub that has been created, add the following code: PrintDocument1.Print()
  3. Picture 9 of How to Create a Print Preview Control in Visual Basic
    Go back to the form and double-click on "PrintDocument1" located underneath the form.
    1. You will be taken back to the page for coding.
    2. A Private Sub will be created called "PrintDocument1_PrintPage". Place the following code into the Sub: Dim font1 As New Font("arial", 16, FontStyle.Regular) e.Graphics.DrawString(RichTextBox1.Text, font1, Brushes.Black, 100, 100)
Part 3 of 3:

Debugging and Testing

  1. Picture 10 of How to Create a Print Preview Control in Visual Basic
    Click "Start" to begin the debugging and testing process.
    1. If you have followed part one and two of this article you should encounter no errors.
  2. Picture 11 of How to Create a Print Preview Control in Visual Basic
    Test to see if the Print Preview function works.
  3. Picture 12 of How to Create a Print Preview Control in Visual Basic
    Test to see if the Print function works.
  4. Picture 13 of How to Create a Print Preview Control in Visual Basic
    Clean up your code. After the debugging and testing process has completed with no critical errors, be sure to clean up your code. Make it look tidy and professional.
Update 05 March 2020
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile