PrintingDemoMainForm.vb

  1: ' This is a demo of using Page Setup, Print preview, and Print dialog boxes.
  2: '
  3: ' Written 3/2010 by Wayne Pollock, Tampa Florida USA
  4: 
  5: Public Class PrintingDemoMainForm
  6:     Private PageNumber As Integer = 0
  7: 
  8:     ' Declare a variable to hold the portion of the document that needs to be printed:
  9:     Private stringToPrint As String
 10: 
 11:     Private Sub PrintingDemoMainForm_Load(ByVal sender As Object, _
 12:                                           ByVal e As System.EventArgs) _
 13:                                           Handles Me.Load
 14:         ' Initialize the Page Setup dialog with default printing settings:
 15:         With PageSetupDialog1
 16:             ' Setup the Page Setup dialog control with default values:
 17:             .PageSettings = New System.Drawing.Printing.PageSettings
 18:             .PrinterSettings = New System.Drawing.Printing.PrinterSettings
 19: 
 20:             ' Customize (but without additional effort, help, network printing
 21:             ' won't work:
 22:             .AllowPrinter = True
 23:             .ShowHelp = False  'You need to handle the HelpRequested event for this
 24:             .ShowNetwork = True
 25:         End With
 26: 
 27:         ' Customize the PrintDialog and PrintPreviewdialog:
 28:         PrintDialog1.AllowPrintToFile = True
 29:         With PrintPreviewDialog1
 30:             .UseAntiAlias = True
 31:             .Text = "Print Preview"
 32:             .Document = PrintDocument1
 33:             .Height = 700
 34:             .Width = 900
 35: 
 36:             ' This sets the zoom factor, but doesn't change the checked "auto"
 37:             ' choice:
 38:             .PrintPreviewControl.Zoom = 1.0  ' 100%
 39: 
 40:             ' Ugliest code contest winner!  This selects the "100%" zoom in the
 41:             '  drop-down list (this is undocumented and can stop working anytime):
 42:             Dim toolstrip1 As ToolStrip = .Controls.Item("ToolStrip1")
 43:             Dim zoomCtrl As ToolStripSplitButton = _
 44:                 toolstrip1.Items.Item("zoomToolStripSplitButton")
 45:             zoomCtrl.DropDown.Items.Item(4).PerformClick()
 46:         End With
 47:     End Sub
 48: 
 49:     ' This method isn't really needed for this simple case, but using a reader
 50:     ' can be useful in general, so I thought I'd show how here:
 51:     Private Sub ReadDocument()
 52:         PrintDocument1.DocumentName = "VB.net Printing Demo"
 53: 
 54:         ' Set up a StringReader to fetch lines from the textbox:
 55:         ' (Note you can use IO.StreamReader to read from a file)
 56:         Using reader As New IO.StringReader(TextBox1.Text)
 57:             stringToPrint = reader.ReadToEnd()
 58:         End Using
 59:     End Sub
 60: 
 61:     Private Sub PrintToolStripMenuItem1_Click(ByVal sender As System.Object, _
 62:                                                 ByVal e As System.EventArgs) _
 63:                                                 Handles PrintToolStripMenuItem1.Click
 64:         If PrintDialog1.ShowDialog() = DialogResult.OK Then
 65: 
 66:             ' Reset the page number:
 67:             PageNumber = 1
 68:             ' Reset the text to be printed:
 69:             ReadDocument()
 70: 
 71:             ' Now ready to print:
 72:             PrintDocument1.Print()
 73:         End If
 74:     End Sub
 75: 
 76:     ' This is the method that gets called for each page to be printed:
 77:     Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
 78:                         ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
 79:                         Handles PrintDocument1.PrintPage
 80:         Dim charactersOnPage As Integer = 0
 81:         Dim linesPerPage As Integer = 0
 82:         Dim thefont As Font = TextBox1.Font
 83: 
 84:         ' Sets the value of charactersOnPage and linesPerPage to the number
 85:         ' of characters and lines of stringToPrint that will fit within the
 86:         ' bounds of the page:
 87:         ' (To display a header of (say) 1 one plus 1 line of margin, get the
 88:         ' font character height and subtract that from the MarginBounds.  Then
 89:         ' pass the custom MarginBounds.Size to MeasureString.)
 90:         e.Graphics.MeasureString(stringToPrint, thefont, e.MarginBounds.Size, _
 91:             StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
 92: 
 93:         ' Use DrawString to draw the header, at the top (using e.MarginBounds).
 94: 
 95:         ' Draw the string within the bounds of the page, automatically line-wrapping:
 96:         '(If a header is used, use the custom Marginbounds.)
 97:         e.Graphics.DrawString(stringToPrint, thefont, Brushes.Black, _
 98:             e.MarginBounds, StringFormat.GenericTypographic)
 99: 
100:         ' Remove the portion of the string that has been printed:
101:         stringToPrint = stringToPrint.Substring(charactersOnPage)
102: 
103:         ' Check to see if more pages are to be printed:
104:         e.HasMorePages = stringToPrint.Length > 0
105:     End Sub
106: 
107:     ' Display the "Page Setup..." dialog:
108:     Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, _
109:                                              ByVal e As System.EventArgs) _
110:                                              Handles PrintToolStripMenuItem.Click
111: 
112:         PageSetupDialog1.ShowDialog()
113:     End Sub
114: 
115:     Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, _
116:                                         ByVal e As System.EventArgs) _
117:                                         Handles PrintPreviewToolStripMenuItem.Click
118:         ReadDocument()  ' Sets StringToPrint.
119:         PrintPreviewDialog1.ShowDialog()
120:     End Sub
121: 
122:     ' This is the handler for the "invisible" exit button, added to the form
123:     ' to allow this as the form's cancel button:
124:     Private Sub ExitBtn_Click(ByVal sender As System.Object, _
125:                               ByVal e As System.EventArgs) _
126:                               Handles ExitBtn.Click
127:         ExitToolStripMenuItem_Click(sender, e)
128:     End Sub
129: 
130:     ' Allow the user to change the font (used for both display and printing):
131:     Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
132:         If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
133:             TextBox1.Font = FontDialog1.Font
134:         End If
135:     End Sub
136: 
137:     Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, _
138:                     ByVal e As System.EventArgs) _
139:                     Handles ExitToolStripMenuItem.Click
140:         Me.Close()
141:     End Sub
142: 
143: End Class