Show PrintPreview Dialog before print out a document : Print Preview Dialog « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » Print Preview Dialog 
23.50.1.Show PrintPreview Dialog before print out a document
Show PrintPreview Dialog before print out a document
using System;        
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PrintExample
{
  /// <summary>
  /// Summary description for PrintPreviewDialog.
  /// </summary>
  public class PrintPreviewDialog : System.Windows.Forms.Form
  {
    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.Drawing.Printing.PrintDocument printDocument1;
    private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
    private System.Windows.Forms.PrintDialog printDialog1;
    private System.Windows.Forms.MenuItem menuItem2;
    private System.Windows.Forms.MenuItem menuItem3;
    private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
    private System.Windows.Forms.MenuItem menuItem4;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public PrintPreviewDialog()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Disposebool disposing )
    {
      ifdisposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      base.Disposedisposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.mainMenu1 = new System.Windows.Forms.MainMenu();
      this.menuItem1 = new System.Windows.Forms.MenuItem();
      this.menuItem2 = new System.Windows.Forms.MenuItem();
      this.menuItem3 = new System.Windows.Forms.MenuItem();
      this.printDocument1 = new System.Drawing.Printing.PrintDocument();
      this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
      this.printDialog1 = new System.Windows.Forms.PrintDialog();
      this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
      this.menuItem4 = new System.Windows.Forms.MenuItem();
      // 
      // mainMenu1
      // 
      this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.menuItem1});
      // 
      // menuItem1
      // 
      this.menuItem1.Index = 0;
      this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.menuItem4,
                                            this.menuItem2,
                                            this.menuItem3});
      this.menuItem1.Text = "File";
      // 
      // menuItem2
      // 
      this.menuItem2.Index = 1;
      this.menuItem2.Text = "Print";
      this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
      // 
      // menuItem3
      // 
      this.menuItem3.Index = 2;
      this.menuItem3.Text = "Print preview";
      this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
      // 
      // printDocument1
      // 
      this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
      // 
      // printPreviewDialog1
      // 
      this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(00);
      this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(00);
      this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400300);
      this.printPreviewDialog1.Document = this.printDocument1;
      this.printPreviewDialog1.Enabled = true;
      this.printPreviewDialog1.Location = new System.Drawing.Point(27213);
      this.printPreviewDialog1.MaximumSize = new System.Drawing.Size(00);
      this.printPreviewDialog1.Name = "printPreviewDialog1";
      this.printPreviewDialog1.Opacity = 1;
      this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
      this.printPreviewDialog1.Visible = false;
      // 
      // printDialog1
      // 
      this.printDialog1.Document = this.printDocument1;
      // 
      // pageSetupDialog1
      // 
      this.pageSetupDialog1.Document = this.printDocument1;
      // 
      // menuItem4
      // 
      this.menuItem4.Index = 0;
      this.menuItem4.Text = "Page setup";
      this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
      // 
      // PrintPreviewDialog
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(292266);
      this.Menu = this.mainMenu1;
      this.Name = "PrintPreviewDialog";
      this.Text = "Print Example";

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new PrintPreviewDialog());
    }

    private int currentPage;

    private void menuItem2_Click(object sender, System.EventArgs e)
    {
      if printDialog1.ShowDialogthis == DialogResult.OK )
      {
        currentPage = 1;
        printDocument1.Print();
      }
    }

    private void menuItem3_Click(object sender, System.EventArgs e)
    {
      currentPage = 1;
      printPreviewDialog1.ShowDialogthis );
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
      e.Graphics.DrawString"Page " + currentPage, new Font"Times New Roman"15 )
        Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top );
      currentPage++;
      if currentPage > )
      {
        e.HasMorePages = false;
      }
      else
      {
        e.HasMorePages = true;
      }
    }

    private void menuItem4_Click(object sender, System.EventArgs e)
    {
      pageSetupDialog1.ShowDialogthis );
    }
  }
}
23.50.Print Preview Dialog
23.50.1.Show PrintPreview Dialog before print out a documentShow PrintPreview Dialog before print out a document
23.50.2.Print Preview Dialog for printing text filePrint Preview Dialog for printing text file
23.50.3.Output the PrinterSettings information
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.