Home

Windows Printing: The Print Dialog Box

     

Description

Printing is the ability to render, on paper, the result of a document or the contents of various controls. This is performed using an external device called a printer peripheral or simply a printer. To do this, users need access to a printer device.

One of the ways users print consists of sending the document to the printer. To directly send a document to the printer, you need to make sure that the control, whose value needs to be printed, supports printing. To accommodate the users of your application, you can provide a menu item or a button they would click. An example of such a button would be Print . To print, the user can click this button. With this type of printing, when the user decides to print, the whole document would be printed "as is", in color if the document is colored and if the printer supports colors. If there is more than one printer, the computer would use what is known as the default printer.

If you want users to be able to configure or customize the printing process, Microsoft Windows provides a common dialog box called Print. Here is an example:

The Print dialog box allows a user to select a printer if more than one is available. The user can decide either to print the whole document, to print a range of pages, or to print a portion of the document that was previously selected. The user can also decide on the number of copies to print from the document, the range specified, or the selected portion. Furthermore, the user can access the particular characteristics of the selected printer and specify how the printer should perform the job. For example, if the selected printer can print in color and the document is in color but the user wants to print in black and white, he or she can specify this using the Properties button.

Providing a Printer

To provide the users with the ability to customize printing through the Print dialog box, you can add a PrintDialog object PrintDialog from the Dialogs section of the Toolbox to your form. The PrintDialog control is implemented through the PrintDialog class of the System.Windows.Forms namespace. To programmatically create a PrintDialog object, you can declare a variable of type PrinterDialog. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Button btnPrint;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        btnPrint  = new Button ();
        btnPrint.Location = new Point(12, 12);
        btnPrint.Text = "&Print...";
        btnPrint.Click += new EventHandler(btnPrintDocument);

        Controls.Add(btnPrint);
    }

    void btnPrintDocument(object sender, EventArgs e)
    {
        PrintDialog dlgPrint = new PrintDialog();
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

To present the Print dialog box to the user, you can call its ShowDialog() method. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Button btnPrint;
    PrintDialog dlgPrint;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        btnPrint = new Button();
        btnPrint.Location = new Point(12, 12);
        btnPrint.Text = "&Print...";
        btnPrint.Click += new EventHandler(btnPrintDocument);

        dlgPrint = new PrintDialog();
        Controls.Add(btnPrint);
        Text = "Printing";
    }

    void btnPrintDocument(object sender, EventArgs e)
    {
        dlgPrint.ShowDialog();
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Exercise());
    }
}
 

Home Copyright © 2014, FunctionX