/home/wpollock1/public_html/VB/PrintingDemoMainForm.vb

' This is a demo of using Page Setup, Print preview, and Print dialog boxes.
'
' Written 3/2010 by Wayne Pollock, Tampa Florida USA

Public Class PrintingDemoMainForm
    Private PageNumber As Integer = 0

    ' Declare a variable to hold the portion of the document that needs to be printed:
    Private stringToPrint As String

    Private Sub PrintingDemoMainForm_Load(ByVal sender As Object, _
                                          ByVal e As System.EventArgs) _
                                          Handles Me.Load
        ' Initialize the Page Setup dialog with default printing settings:
        With PageSetupDialog1
            ' Setup the Page Setup dialog control with default values:
            .PageSettings = New System.Drawing.Printing.PageSettings
            .PrinterSettings = New System.Drawing.Printing.PrinterSettings

            ' Customize (but without additional effort, help, network printing
            ' won't work:
            .AllowPrinter = True
            .ShowHelp = False  'You need to handle the HelpRequested event for this
            .ShowNetwork = True
        End With

        ' Customize the PrintDialog and PrintPreviewdialog:
        PrintDialog1.AllowPrintToFile = True
        With PrintPreviewDialog1
            .UseAntiAlias = True
            .Text = "Print Preview"
            .Document = PrintDocument1
            .Height = 700
            .Width = 900

            ' This sets the zoom factor, but doesn't change the checked "auto"
            ' choice:
            .PrintPreviewControl.Zoom = 1.0  ' 100%

            ' Ugliest code contest winner!  This selects the "100%" zoom in the
            '  drop-down list (this is undocumented and can stop working anytime):
            Dim toolstrip1 As ToolStrip = .Controls.Item("ToolStrip1")
            Dim zoomCtrl As ToolStripSplitButton = _
                toolstrip1.Items.Item("zoomToolStripSplitButton")
            zoomCtrl.DropDown.Items.Item(4).PerformClick()
        End With
    End Sub

    ' This method isn't really needed for this simple case, but using a reader
    ' can be useful in general, so I thought I'd show how here:
    Private Sub ReadDocument()
        PrintDocument1.DocumentName = "VB.net Printing Demo"

        ' Set up a StringReader to fetch lines from the textbox:
        ' (Note you can use IO.StreamReader to read from a file)
        Using reader As New IO.StringReader(TextBox1.Text)
            stringToPrint = reader.ReadToEnd()
        End Using
    End Sub

    Private Sub PrintToolStripMenuItem1_Click(ByVal sender As System.Object, _
                                                ByVal e As System.EventArgs) _
                                                Handles PrintToolStripMenuItem1.Click
        If PrintDialog1.ShowDialog() = DialogResult.OK Then

            ' Reset the page number:
            PageNumber = 1
            ' Reset the text to be printed:
            ReadDocument()

            ' Now ready to print:
            PrintDocument1.Print()
        End If
    End Sub

    ' This is the method that gets called for each page to be printed:
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
                        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
                        Handles PrintDocument1.PrintPage
        Dim charactersOnPage As Integer = 0
        Dim linesPerPage As Integer = 0
        Dim thefont As Font = TextBox1.Font

        ' Sets the value of charactersOnPage and linesPerPage to the number
        ' of characters and lines of stringToPrint that will fit within the
        ' bounds of the page:
        ' (To display a header of (say) 1 one plus 1 line of margin, get the
        ' font character height and subtract that from the MarginBounds.  Then
        ' pass the custom MarginBounds.Size to MeasureString.)
        e.Graphics.MeasureString(stringToPrint, thefont, e.MarginBounds.Size, _
            StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

        ' Use DrawString to draw the header, at the top (using e.MarginBounds).

        ' Draw the string within the bounds of the page, automatically line-wrapping:
        '(If a header is used, use the custom Marginbounds.)
        e.Graphics.DrawString(stringToPrint, thefont, Brushes.Black, _
            e.MarginBounds, StringFormat.GenericTypographic)

        ' Remove the portion of the string that has been printed:
        stringToPrint = stringToPrint.Substring(charactersOnPage)

        ' Check to see if more pages are to be printed:
        e.HasMorePages = stringToPrint.Length > 0
    End Sub

    ' Display the "Page Setup..." dialog:
    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, _
                                             ByVal e As System.EventArgs) _
                                             Handles PrintToolStripMenuItem.Click

        PageSetupDialog1.ShowDialog()
    End Sub

    Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, _
                                        ByVal e As System.EventArgs) _
                                        Handles PrintPreviewToolStripMenuItem.Click
        ReadDocument()  ' Sets StringToPrint.
        PrintPreviewDialog1.ShowDialog()
    End Sub

    ' This is the handler for the "invisible" exit button, added to the form
    ' to allow this as the form's cancel button:
    Private Sub ExitBtn_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles ExitBtn.Click
        ExitToolStripMenuItem_Click(sender, e)
    End Sub

    ' Allow the user to change the font (used for both display and printing):
    Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
        If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Font = FontDialog1.Font
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) _
                    Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

End Class