How to Create a Print Preview Control in Visual Basic
Adjust the form to the size you require., Add a RichTextBox to your form., Add two Buttons to your form., Name the Buttons "Print" and "Print Preview"., Add the controls: "Print Document" and "PrintPreviewDialog" to your form., Click on...
Step-by-Step Guide
-
Step 1: Adjust the form to the size you require.
It doesn't matter what size the form is, providing it can fit a large textbox and two buttons. -
Step 2: Add a RichTextBox to your form.
You can add a RichTextBox by double-clicking or dragging it onto the form.
Adjust the size of the RichTextBox to your requirements. , Ideally, place the two Buttons near the RichTextBox.
Again, you can add Buttons to your form by double-clicking or dragging onto the form. , You can change the text for the buttons in the properties box. , These will not be visible on the form as they perform in the background. , You can change this in the properties box after clicking on PrintPreviewDialog. , This will bring up the page for coding.
A Private Sub has already been created and awaiting code to tell it what to do when the Print Preview button is clicked.
Add the following code into the Private Sub:
PrintPreviewDialog1.ShowDialog() , You will be taken back to the page for coding.
In the Private Sub that has been created, add the following code:
PrintDocument1.Print() , You will be taken back to the page for coding.
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) , If you have followed part one and two of this article you should encounter no errors. ,,, 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. -
Step 3: Add two Buttons to your form.
-
Step 4: Name the Buttons "Print" and "Print Preview".
-
Step 5: Add the controls: "Print Document" and "PrintPreviewDialog" to your form.
-
Step 6: Click on PrintPreviewDialog and change the "Document" to "PrintDocument1".
-
Step 7: Double-click on your "Print Preview" button.
-
Step 8: Go back to the form and double-click on your "Print" button.
-
Step 9: Go back to the form and double-click on "PrintDocument1" located underneath the form.
-
Step 10: Click "Start" to begin the debugging and testing process.
-
Step 11: Test to see if the Print Preview function works.
-
Step 12: Test to see if the Print function works.
-
Step 13: Clean up your code.
Detailed Guide
It doesn't matter what size the form is, providing it can fit a large textbox and two buttons.
You can add a RichTextBox by double-clicking or dragging it onto the form.
Adjust the size of the RichTextBox to your requirements. , Ideally, place the two Buttons near the RichTextBox.
Again, you can add Buttons to your form by double-clicking or dragging onto the form. , You can change the text for the buttons in the properties box. , These will not be visible on the form as they perform in the background. , You can change this in the properties box after clicking on PrintPreviewDialog. , This will bring up the page for coding.
A Private Sub has already been created and awaiting code to tell it what to do when the Print Preview button is clicked.
Add the following code into the Private Sub:
PrintPreviewDialog1.ShowDialog() , You will be taken back to the page for coding.
In the Private Sub that has been created, add the following code:
PrintDocument1.Print() , You will be taken back to the page for coding.
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) , If you have followed part one and two of this article you should encounter no errors. ,,, 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.
About the Author
Elizabeth Wells
Experienced content creator specializing in home improvement guides and tutorials.
Rate This Guide
How helpful was this guide? Click to rate: