Public Class frmPrint
    Inherits System.Windows.Forms.Form
    Private aSQL As String() = New String() {}
    Private i As Integer

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        PrintDialog1.Document = PrintDocument1
        PageSetupDialog1.Document = PrintDocument1

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
    Friend WithEvents PrintDialog1 As System.Windows.Forms.PrintDialog
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents PageSetupDialog1 As System.Windows.Forms.PageSetupDialog
    Friend WithEvents PrintPreviewControl1 As System.Windows.Forms.PrintPreviewControl
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument()
        Me.PrintDialog1 = New System.Windows.Forms.PrintDialog()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.PageSetupDialog1 = New System.Windows.Forms.PageSetupDialog()
        Me.PrintPreviewControl1 = New System.Windows.Forms.PrintPreviewControl()
        Me.Button4 = New System.Windows.Forms.Button()
        Me.Button5 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'PrintDocument1
        '
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(16, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(88, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Preview"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(16, 72)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(88, 24)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Print"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(16, 112)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(88, 24)
        Me.Button3.TabIndex = 2
        Me.Button3.Text = "Page Setup"
        '
        'PrintPreviewControl1
        '
        Me.PrintPreviewControl1.Location = New System.Drawing.Point(272, 8)
        Me.PrintPreviewControl1.Name = "PrintPreviewControl1"
        Me.PrintPreviewControl1.Size = New System.Drawing.Size(600, 408)
        Me.PrintPreviewControl1.TabIndex = 3
        Me.PrintPreviewControl1.Zoom = 0.3
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(784, 432)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(40, 16)
        Me.Button4.TabIndex = 4
        Me.Button4.Text = "<"
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(824, 432)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(40, 16)
        Me.Button5.TabIndex = 5
        Me.Button5.Text = ">"
        '
        'frmPrint
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(880, 461)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button5, Me.Button4, Me.PrintPreviewControl1, Me.Button3, Me.Button2, Me.Button1})
        Me.Name = "frmPrint"
        Me.Text = "frmPrint"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try 'Page Setup
            PageSetupDialog1.AllowOrientation = False
            PageSetupDialog1.AllowPaper = False
            PageSetupDialog1.AllowPrinter = False
            PageSetupDialog1.ShowDialog()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintPreviewControl1.Document = Nothing
        PrintPreviewControl1.Document = PrintDocument1
        PrintPreviewControl1.Columns = 2

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim pFont As Font

        pFont = New Font("Comic Sans MS", 20)
        Dim x, y As Integer
        Dim LM, RM, TM, BM As Integer
        With PrintDocument1.DefaultPageSettings.Margins
            LM = .Left
            RM = .Right
            TM = .Top
            BM = .Bottom
        End With

        'Only Print Selected Pages
        If PrintDocument1.PrinterSettings.PrintRange = Drawing.Printing.PrintRange.SomePages Then

            'Start on the first page seleted
            If i + 1 < PrintDocument1.PrinterSettings.FromPage Then _
                i = PrintDocument1.PrinterSettings.FromPage - 1

            e.Graphics.DrawString(aSQL(i), pFont, Brushes.Black, LM, TM)

        Else
            'Pring All Pages
            e.Graphics.DrawString(aSQL(i), pFont, Brushes.Black, LM, TM)
        End If

        'Check for End of File or End Of Printing Selection.
        If i + 1 > aSQL.GetUpperBound(0) Or _
            (PrintDocument1.PrinterSettings.PrintRange = Drawing.Printing.PrintRange.SomePages _
             And i + 1 >= PrintDocument1.PrinterSettings.ToPage) Then

            e.HasMorePages = False
        Else
            i += 1
            e.HasMorePages = True
        End If


    End Sub

    Private Sub frmPrint_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReDim Preserve aSQL(2)
        aSQL(0) = "First Page - Start Page"
        aSQL(1) = "Second Page - Middle Page"
        aSQL(2) = "Third Page - Last Page"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        i = 0
        PrintDialog1.AllowSomePages = True

        'If you want to print to file then set up a printer as a printer set to the port "file"
        PrintDialog1.AllowPrintToFile = False


        If PrintDialog1.ShowDialog = DialogResult.OK Then
            PrintDocument1.Print()
        End If

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        PrintPreviewControl1.StartPage = PrintPreviewControl1.StartPage - PrintPreviewControl1.Columns
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        PrintPreviewControl1.StartPage = PrintPreviewControl1.StartPage + PrintPreviewControl1.Columns
    End Sub
End Class