Printing a Document

One of the key features of any full-fledged desktop application is the ability to print. Let's add another choice to the Welcome Form menu, mnuFile with the text File. While creating the menu choice, use the Properties window to create mnuFile_Click and add a single line:

Private Sub mnuFile_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFile.Click
   frmText.Show()
End Sub

As you can guess, you'll now want to add a new form, frmText to the project. Resize the new form to 700,600 and set its text attribute to "Text Form."

This form will have two controls, a RichTextBox and a menu. The menu name will be "File" (mnuFile) and will have sub-menu items of Open, Save, and Print, as shown in Figure 3-14.

Items Collection EditorItems Collection Editor Items Collection Editor

Figure 3-14. Items Collection Editor Items Collection Editor

After your menu is set up, drag a RichTextBox control onto the form and set its size to 668,512 and its location to 12,42.

Drag an OpenFileDialog and a SaveFileDialog onto your tool strip, along with a PrintDocument and a PrintDialog control, leaving their default names as they are. Implement the mnuFileOpen_Click event handler first, as shown in Example 3-15.

Example 3-15. File menu Open item Click event handler

Private Sub mnuFileOpen_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles mnuFileOpen.Click ' set the initial directory in which to look for ...

Get Programming Visual Basic 2005 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.