Using the PrintDialog Control


Imports System.Drawing.Printing

Public Class Form1

    Private printDoc As New PrintDocument()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler printDoc.PrintPage, AddressOf Me.PrintPageHandler
    End Sub

    Private Sub PrintPageHandler(sender As Object, e As PrintPageEventArgs)
        e.Graphics.DrawString("Hello from .NET PrintDialog!", New Font("Arial", 12), Brushes.Black, 100, 100)
    End Sub

    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
        Try
            Using dlg As New PrintDialog()
                dlg.Document = printDoc
                dlg.AllowSomePages = True
                dlg.UseEXDialog = True ' Try extended dialog (WinXP+)

                Dim result = dlg.ShowDialog()
                If result = DialogResult.OK Then
                    printDoc.Print()
                    MessageBox.Show("Printing started.", "Print")
                Else
                    MessageBox.Show("User cancelled print dialog.", "Cancelled")
                End If
            End Using
        Catch ex As Exception
            MessageBox.Show("Error showing print dialog: " & ex.Message, "Error")
        End Try
    End Sub
End Class

Download 'Using the PrintDialog Control':

📥 Download using-the-printdialog-control.vb