Show PrintPreview Dialog before print out a document : Print Preview Dialog : GUI Windows Forms C# Examples


C# Examples » GUI Windows Forms » Print Preview Dialog »

 

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  Dispose(  bool  disposing  )
        {
            if(  disposing  )
            {
                if  (components  !=  null)  
                {
                    components.Dispose();
                }
            }
            base.Dispose(  disposing  );
        }

        #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(0,  0);
            this.printPreviewDialog1.AutoScrollMinSize  =  new  System.Drawing.Size(0,  0);
            this.printPreviewDialog1.ClientSize  =  new  System.Drawing.Size(400,  300);
            this.printPreviewDialog1.Document  =  this.printDocument1;
            this.printPreviewDialog1.Enabled  =  true;
            this.printPreviewDialog1.Location  =  new  System.Drawing.Point(272,  13);
            this.printPreviewDialog1.MaximumSize  =  new  System.Drawing.Size(0,  0);
            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(5,  13);
            this.ClientSize  =  new  System.Drawing.Size(292,  266);
            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.ShowDialog(  this  )  ==  DialogResult.OK  )
            {
                currentPage  =  1;
                printDocument1.Print();
            }
        }

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

        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  >  3  )
            {
                e.HasMorePages  =  false;
            }
            else
            {
                e.HasMorePages  =  true;
            }
        }

        private  void  menuItem4_Click(object  sender,  System.EventArgs  e)
        {
            pageSetupDialog1.ShowDialog(  this  );
        }
    }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo GUI Windows Forms
» Print Preview Dialog