Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2010
Visual Studio
Windows Forms
 How to: Create Standard Windows For...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework 4 - Windows Forms
How to: Create Standard Windows Forms Print Jobs

The foundation of printing in Windows Forms is the PrintDocument component—more specifically, the PrintPage event. By writing code to handle the PrintPage event, you can specify what to print and how to print it.

To create a print job

  1. Add a PrintDocument component to your form.

  2. Write code to handle the PrintPage event.

    You will have to code your own printing logic. Additionally, you will have to specify the material to be printed.

    In the following code example, a sample graphic in the shape of a red rectangle is created in the PrintPage event handler to act as material to be printed.

    Visual Basic
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
       e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500))
    End Sub
    
    C#
    private void printDocument1_PrintPage(object sender, 
    System.Drawing.Printing.PrintPageEventArgs e)
    {
       e.Graphics.FillRectangle(Brushes.Red, 
         new Rectangle(500, 500, 500, 500));
    }
    
    Visual C++
    private:
       void printDocument1_PrintPage(System::Object ^ sender,
          System::Drawing::Printing::PrintPageEventArgs ^ e)
       {
          e->Graphics->FillRectangle(Brushes::Red,
             Rectangle(500, 500, 500, 500));
       }

    (Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.

    C#
    this.printDocument1.PrintPage += new
       System.Drawing.Printing.PrintPageEventHandler
       (this.printDocument1_PrintPage);
    
    Visual C++
    printDocument1->PrintPage += gcnew
       System::Drawing::Printing::PrintPageEventHandler
       (this, &Form1::printDocument1_PrintPage);

    You may also want to write code for the BeginPrint and EndPrint events, perhaps including an integer representing the total number of pages to print that is decremented as each page prints.

    NoteNote

    You can add a PrintDialog component to your form to provide a clean and efficient user interface (UI) to your users. Setting the Document property of the PrintDialog component enables you to set properties related to the print document you are working with on your form. For more information about the PrintDialog component, see PrintDialog Component (Windows Forms).

    For more information about the specifics of Windows Forms print jobs, including how to create a print job programmatically, see PrintPageEventArgs.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker