Print Dialog Box in VB.net – Properties, Methods and Events

In this article is used to show the Print Dialog box in VB.net in an application. 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.

We have learned how to use the Open File Dialog Box in VB.net in the previous lesson. In this lesson we shall learn How To Write a Program for Print Dialog in VB.net.

What is 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.netDescription
1.AllowCurrentPageGets or sets a value indicating whether the Current Page option button is displayed.
2.AllowPrintToFileGets or sets a value indicating whether the Print to file check box is enabled.
3.AllowSelectionGets or sets a value indicating whether the Selection option button is enabled.
4.AllowSomePagesGets or sets a value indicating whether the Pages option button is enabled.
5.DocumentGets or sets a value indicating the PrintDocument used to obtain PrinterSettings.
6.PrinterSettingsGets or sets the printer settings the dialog box modifies.
7.PrintToFileGets or sets a value indicating whether the Print to file check box is selected.
8.ShowHelpGets or sets a value indicating whether the Help button is displayed.
9.ShowNetworkGets or sets a value indicating whether the Network button is displayed.
Properties and Description of Print Dialog Box in VB.net

Print Dialog Methods in VB.net

The Print Dialog Box in VB.net has the following Methods:

#Print Dialog Methods in VB.netDescription
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.
Methods and Description of Print Dialog Box in VB.net

Print Dialog Events in VB.net

The Print Dialog Box in VB.net has the following Events:

#Print Dialog Events in VB.netDescription
1.DisposedWhen control or component is terminated by calling the Dispose() method, a Dispose event occurs.
2.HelpRequestWhen a user clicks the Help button of the dialog box, the HelpRequest event is called.
Events and Descriptions of Print Dialog Box in VB.net

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:

Print Dialog Box in VB.net Output
Print Dialog Box in VB.net Output

Write something in the textbox. As shown below, we wrote the following text in the text box.

Print Dialog Box in VB.net Insert Text
Print Dialog Box in VB.net Insert Text

After you write the text, click the Print button to print the document and set the printer settings, as shown below.

Print Dialog Box in VB.net Settings
Print Dialog Box Settings

We can also see a preview of the document by clicking the Preview button, which brings up the image below.

Print Dialog Box in VB.net Preview Text
Print Dialog Box Preview Text

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.


Leave a Comment