One Step Further: Adding Print Preview and Page Setup Dialog Boxes


One Step Further: Adding Print Preview and Page Setup Dialog Boxes

The Print File application is ready to handle several printing tasks, but its interface isn't as visually compelling as that of a commercial Windows application. You can make your program more flexible and interesting by adding a few extra dialog box options to supplement the Print dialog box that you experimented with in the previous exercise.

Two additional printing controls are available on the Printing tab of the Toolbox, and they work much like the familiar PrintDialog and OpenFileDialog controls that you've used in this book:

  • The PrintPreviewDialog control displays a custom Print Preview dialog box.

  • The PageSetupDialog control displays a custom Page Setup dialog box.

As with other dialog boxes, you can add these printing controls to your form by using the Toolbox, or you can create them programmatically.

In the following exercise, you'll add Print Preview and Page Setup dialog boxes to the Print File program you've been working with. In the completed practice files, I've named this project Print Dialogs so that you can distinguish the code of the two projects, but you can add the dialog box features directly to the Print File project if you want.

Add PrintPreviewDialog and PageSetupDialog controls

  1. If you didn't complete the previous exercise, open the Print File project from the c:\vb05sbs\chap17\print file folder.

    The Print File project is the starting point for this project.

  2. Display the form, and then use the Button control to add two additional buttons to the top of the form.

  3. Double-click the PrintPreviewDialog control on the Printing tab of the Toolbox.

    A print preview dialog object is added to the component tray.

  4. Double-click the PageSetupDialog control on the Printing tab of the Toolbox.

    A page setup dialog object is added to the component tray. If the objects in the component tray obscure one another, you can drag them to a better (more visible) location, or you can right-click the component tray and select Line Up Icons.

  5. Set the following properties for the button objects on the form:

    Object

    Property

    Setting

    Button1

    Name

    Enabled

    Text

    btnSetup

    False

    “Page Setup”

    Button2

    Name

    Enabled

    Text

    btnPreview

    False

    “Print Preview”

    Your form looks like this:

    graphic

  6. Double-click the Page Setup button (btnSetup) to display the btnSetup_Click event procedure in the Code Editor.

  7. Type the following program code:

    Try      'Load page settings and display page setup dialog box      PageSetupDialog1.PageSettings = PrintPageSettings      PageSetupDialog1.ShowDialog()  Catch ex As Exception      'Display error message      MessageBox.Show(ex.Message)  End Try

    The code for creating a Page Setup dialog box in this program is quite simple because the PrintPageSettings variable has already been defined at the top of the form. This variable holds the current page definition information, and when it's assigned to the PageSettings property of the PageSetupDialog1 object, the ShowDialog method automatically loads a dialog box that allows the user to modify what the program has selected as the default page orientation, margins, and so on. The Try…Catch error handler simply handles any errors that might occur when the ShowDialog method is used.

  8. Display the form again, and then double-click the Print Preview button (btnPreview) to display the btnPreview_Click event procedure.

  9. Type the following program code:

    Try      'Specify current page settings      PrintDocument1.DefaultPageSettings = PrintPageSettings      'Specify document for print preview dialog box and show      StringToPrint = RichTextBox1.Text      PrintPreviewDialog1.Document = PrintDocument1      PrintPreviewDialog1.ShowDialog()  Catch ex As Exception      'Display error message      MessageBox.Show(ex.Message)  End Try

    In a similar way, the btnPreview_Click event procedure assigns the PrintPageSettings variable to the DefaultPageSettings property of the PrintDocument1 object, and then it copies the text in the rich text box object to the StringToPrint variable and opens the Print Preview dialog box. Print Preview automatically uses the page settings data to display a visual representation of the document as it will be printed—you don't need to display this information manually.

    Now you'll make a slight modification to the program code in the btnOpen_Click event procedure.

  10. Scroll up to the btnOpen_Click event procedure in the Code Editor.

    This is the procedure that displays the Open dialog box, opens a text file, and enables the printing buttons. Because you just added the Page Setup and Print Preview buttons, you have to add program code to enable those two printing buttons as well.

  11. Scroll to the bottom of the event procedure, just before the final Catch code block, and locate the following program statement:

       btnPrint.Enabled = True

  12. Below that statement, add the following lines of code:

    btnSetup.Enabled = True  btnPreview.Enabled = True

    Now your program will enable the print buttons when there's a document available to print.

  13. Click the Save All button on the toolbar to save your changes.

Test the Page Setup and Print Preview features

    TIP
    The complete Print Dialogs program is located in the c:\vb05sbs\chap17\print dialogs folder.

  1. Click the Start Debugging button on the toolbar.

    The program opens, with only the first button object enabled.

  2. Click the Open button, and then open the longfile.txt file in the c:\vb05sbs\chap17 folder.

    The remaining three button objects are now enabled, as shown here:

    graphic

  3. Click the Page Setup button.

    Your program displays the Page Setup dialog box, as shown here:

    graphic

    Page Setup provides numerous useful options, including the ability to change the paper size and source, the orientation of the printing (Portrait or Landscape), and the page margins (Left, Right, Top, and Bottom).

  4. Change the Left margin to 2, and then click OK.

    The left margin will now be 2 inches.

  5. Click the Print Preview button.

    Your program displays the Print Preview dialog box, as shown in the following illustration:

    graphic

    If you've used the Print Preview command in Microsoft Word or Microsoft Excel, you will recognize several of the buttons and preview features in this Print Preview dialog box. The Zoom, One Page, Two Pages, Three Pages, Four Pages, Six Pages, and Page Select Box controls all work automatically in the dialog box. No program code is required to make them operate.

  6. Click the Four Pages button to display your document four pages at a time.

  7. Click the Maximize button on the Print Preview title bar to make the window full size.

  8. Click the Zoom arrow, click 150 percent, and expand the size of the window a little.

    Your screen looks like this:

    graphic

  9. Click the One Page button, and then click the Up arrow in the Page Select box to view pages 2 through 4.

    As you can see, this Print Preview window is quite impressive—and you incorporated it into your program with just a few lines of code!

  10. If you want to test printing the entire document again, click the Print button.

  11. When you're finished experimenting, click the Close button to close the Print Preview dialog box, and then click the Close button to close the program.

You're done working with printers for now.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net