It is an important dialog control that lets the user choose sections of a document and then choose a printer to print pages from a Windows Forms application.
In VB.net Print Dialog Box, a user can also specify the range of pages to print, the printer to use, and the number of copies to print.
And we can show the PrintDialog control in a Windows form by using the ShowDialog() method.
What is a Print Dialog Box in VB.net?
A Print Dialog in VB.net is used to open up the Windows Print Dialog.
A typical Open File dialog, where you can choose a printer from the list of available printers, set printer properties, set print range, number of pages, number of copies, and so on.
Clicking the OK button sends the document to the printer.
Print Dialog Properties in VB.net
The Print Dialog Box in VB.net has the following Properties:
# | Print Dialog Properties in VB.net | Description |
---|---|---|
1. | AllowCurrentPage | Gets or sets a value indicating whether the Current Page option button is displayed. |
2. | AllowPrintToFile | Gets or sets a value indicating whether the Print to file check box is enabled. |
3. | AllowSelection | Gets or sets a value indicating whether the Selection option button is enabled. |
4. | AllowSomePages | Gets or sets a value indicating whether the Pages option button is enabled. |
5. | Document | Gets or sets a value indicating the PrintDocument used to obtain PrinterSettings. |
6. | PrinterSettings | Gets or sets the printer settings the dialog box modifies. |
7. | PrintToFile | Gets or sets a value indicating whether the Print to file check box is selected. |
8. | ShowHelp | Gets or sets a value indicating whether the Help button is displayed. |
9. | ShowNetwork | Gets or sets a value indicating whether the Network button is displayed. |
Print Dialog Methods in VB.net
The Print Dialog Box in VB.net has the following Methods:
# | Print Dialog Methods in VB.net | Description |
---|---|---|
1. | ShowDialog() | The ShowDialog () method is used to run a common dialog box with the default setting. |
2. | Reset() | The Reset() method is used to reset all changes to their default values. |
3. | Dispose() | The Dispose() method is used to free all resources used by the Control or component in the Dialog Box. |
4. | RunDialog() | It is used to override a derived class to create a common dialog box. |
Print Dialog Events in VB.net
The Print Dialog Box in VB.net has the following Events:
# | Print Dialog Events in VB.net | Description |
---|---|---|
1. | Disposed | When control or component is terminated by calling the Dispose() method, a Dispose event occurs. |
2. | HelpRequest | When a user clicks the Help button of the dialog box, the HelpRequest event is called. |
Let’s make a simple program in VB.NET Windows Forms that lets us print and look at the document before we print it.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PrintPreviewDialog1.Document = PrintDocument1
End Sub
Private Sub Button1_Click_4(sender As Object, e As EventArgs) Handles Button1.Click
If RichTextBox1.Text = " " Then
MsgBox("Please write some text...")
Else
PrintPreviewDialog1.ShowDialog()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If PrintDialog1.ShowDialog = DialogResult.OK Then 'Open the print dialog box
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
PrintDocument1.Print() 'print a document
End If
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim font As New Font("Times New Roman", 24, FontStyle.Bold) 'set the font to display
e.Graphics.DrawString(RichTextBox1.Text, font, Brushes.Blue, 100, 100) 'The DrawString() function is used to print letters.
End Sub
End Class
Program Output:
Write something in the textbox. As shown below, we wrote the following text in the text box.
After you write the text, click the Print button to print the document and set the printer settings, as shown below.
We can also see a preview of the document by clicking the Preview button, which brings up the image below.
Summary
A PrintDialog control lets users open the Windows Open File Dialog and select a file to print.
In this article, we went over how to use a Windows Open File Dialog using Microsoft Visual Studio and set its Properties, Methods, and Events in a Windows Forms application.
PREVIOUS
NEXT