O'Reilly logo

Programming Visual Basic 2005 by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 files ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required