--- title: Print Using Print Preview description: Learn how to add print preview services to your application by using the Windows Forms PrintPreviewDialog control. ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "printing [Windows Forms], using print preview" - "printing [Windows Forms], with print preview" - "print preview" ms.assetid: 4a16f7e2-ae10-4485-b0ae-3d558334d0fe --- # How to: Print in Windows Forms Using Print Preview It is very common in Windows Forms programming to offer print preview in addition to printing services. An easy way to add print preview services to your application is to use a control in combination with the event-handling logic for printing a file. ### To preview a text document with a PrintPreviewDialog control 1. Add a , , and two strings to your form. [!code-csharp[System.Drawing.Printing.PrintPreviewExample#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/CS/Form1.cs#1)] [!code-vb[System.Drawing.Printing.PrintPreviewExample#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/VB/Form1.vb#1)] 2. Set the property to the document you wish to print, and open and read the document's contents to the string you added previously. [!code-csharp[System.Drawing.Printing.PrintPreviewExample#2](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/CS/Form1.cs#2)] [!code-vb[System.Drawing.Printing.PrintPreviewExample#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/VB/Form1.vb#2)] 3. As you would for printing the document, in the event handler, use the property of the class and the file contents to calculate lines per page and render the document's contents. After each page is drawn, check to see if it is the last page, and set the property of the accordingly. The event is raised until is `false`. When the document has finished rendering, reset the string to be rendered. Also, make sure the event is associated with its event-handling method. > [!NOTE] > You may have already completed steps 2 and 3 if you have implemented printing in your application. In the following code example, the event handler is used to print the "testPage.txt" file in the same font used on the form. [!code-csharp[System.Drawing.Printing.PrintPreviewExample#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/CS/Form1.cs#3)] [!code-vb[System.Drawing.Printing.PrintPreviewExample#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/VB/Form1.vb#3)] 4. Set the property of the control to the component on the form. [!code-csharp[System.Drawing.Printing.PrintPreviewExample#5](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/CS/Form1.cs#5)] [!code-vb[System.Drawing.Printing.PrintPreviewExample#5](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/VB/Form1.vb#5)] 5. Call the method on the control. You would typically call from the event-handling method of a button. Calling raises the event and renders the output to the control. When the user clicks the print icon on the dialog, the event is raised again, sending the output to the printer instead of the preview dialog. This is why the string is reset at the end of the rendering process in step 3. The following code example shows the event-handling method for a button on the form. This event-handling method calls the methods to read the document and show the print preview dialog. [!code-csharp[System.Drawing.Printing.PrintPreviewExample#4](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/CS/Form1.cs#4)] [!code-vb[System.Drawing.Printing.PrintPreviewExample#4](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/VB/Form1.vb#4)] ## Example [!code-csharp[System.Drawing.Printing.PrintPreviewExample#0](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/CS/Form1.cs#0)] [!code-vb[System.Drawing.Printing.PrintPreviewExample#0](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintPreviewExample/VB/Form1.vb#0)] ## Compiling the Code This example requires: - References to the System, System.Windows.Forms, System.Drawing assemblies. ## See also - [How to: Print a Multi-Page Text File in Windows Forms](how-to-print-a-multi-page-text-file-in-windows-forms.md) - [Windows Forms Print Support](windows-forms-print-support.md) - [More Secure Printing in Windows Forms](../more-secure-printing-in-windows-forms.md)