How to: Complete Windows Forms Print Jobs

Frequently, word processors and other applications that involve printing will provide the option to display a message to users that a print job is complete. You can provide this functionality in your Windows Forms by handling the EndPrint event of the PrintDocument component.

The following procedure requires that you have created a Windows-based application with a PrintDocument component on it, which is the standard way of enabling printing from a Windows-based application. For more information about printing from Windows Forms using the PrintDocument component, see How to: Create Standard Windows Forms Print Jobs.

To complete a print job

  1. Set the DocumentName property of the PrintDocument component.

    PrintDocument1.DocumentName = "MyTextFile"
    
    printDocument1.DocumentName = "MyTextFile";
    
    printDocument1->DocumentName = "MyTextFile";
    
  2. Write code to handle the EndPrint event.

    In the following code example, a message box is displayed, indicating that the document has finished printing.

    Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
       MessageBox.Show(PrintDocument1.DocumentName + " has finished printing.")
    End Sub
    
    private void printDocument1_EndPrint(object sender, 
    System.Drawing.Printing.PrintEventArgs e)
    {
       MessageBox.Show(printDocument1.DocumentName + 
          " has finished printing.");
    }
    
    private:
       void printDocument1_EndPrint(System::Object ^ sender,
          System::Drawing::Printing::PrintEventArgs ^ e)
       {
          MessageBox::Show(String::Concat(printDocument1->DocumentName,
             " has finished printing."));
       }
    

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

    this.printDocument1.EndPrint += new
       System.Drawing.Printing.PrintEventHandler
       (this.printDocument1_EndPrint);
    
    this->printDocument1->EndPrint += gcnew
       System::Drawing::Printing::PrintEventHandler
       (this, &Form1::printDocument1_EndPrint);
    

See Also

Reference

PrintDocument

Other Resources

Windows Forms Print Support