How to Create a Print Preview Control in Visual Basic
Part 1 of 3:
The Form
- 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.
- 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.
- Add two Buttons to your form.
- Ideally, place the two Buttons near the RichTextBox.
- Again, you can add Buttons to your form by double-clicking or dragging onto the form.
- Name the Buttons "Print" and "Print Preview".
- You can change the text for the buttons in the properties box.
- Add the controls: "Print Document" and "PrintPreviewDialog" to your form.
- These will not be visible on the form as they perform in the background.
- Click on PrintPreviewDialog and change the "Document" to "PrintDocument1".
- You can change this in the properties box after clicking on PrintPreviewDialog.
Part 2 of 3:
The Code
- Double-click on your "Print Preview" button.
- 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()
- Go back to the form and double-click on your "Print" button.
- You will be taken back to the page for coding.
- In the Private Sub that has been created, add the following code:
PrintDocument1.Print()
- Go back to the form and double-click on "PrintDocument1" located underneath the form.
- 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)
Part 3 of 3:
Debugging and Testing
- Click "Start" to begin the debugging and testing process.
- If you have followed part one and two of this article you should encounter no errors.
- Test to see if the Print Preview function works.
- Test to see if the Print function works.
- 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.
3.9 ★ | 16 Vote
You should read it
- How to print web pages without ads
- How to print Excel, print Excel spreadsheets fast, standard
- How to Print Double Quotes in Java
- How to print A4 to A5 in PDF simple and fast
- New printing solution with Google Cloud Print
- The print command in Windows
- Form - Form in CSS
- How to cancel a print job on Word, Excel
May be interested
- How to Protect VBA Codemicrosoft's visual basic for applications (vba) is the standard language for writing programs to automate functions and tasks in microsoft office. understand how to protect vba code so others can't sabotage or steal your macros. open the...
- How to Create a Simple Calculator in Visual Basic 6.0this wikihow teaches you how to use microsoft's visual basic 6.0 to create a simple calculator that can add, subtract, multiply, and divide. keep in mind that visual basic 6.0 is no longer used by modern computers, so you'll need to have...
- Learn about Vue routersif you want to create a fully functional front-end application, then vue-router and vue-resource are two great key elements in vuejs. today's article will delve into the vue router.
- Write Happy New Year in 10 different programming languageshappy new year 2019 in more than 10 most popular programming languages is a way for coders to send new year greetings to their friends, relatives and partners during the new year to spring.
- 11 useful tools for testing and optimizing CSS filesby reducing the css file size, the server will take less time to load, making the site faster. using a css inspector capable of cleaning common errors can also help.
- 13 IDEs in the browser that every programmer should knowmany browser-based ides are suitable for programming in the cloud. most of these tools have limitations when compared to offline competitors, but they are slowly improving over time.