Access to a printDialog from a PrintPreviewDialog

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • choupi

    Access to a printDialog from a PrintPreviewDialog

    Hello,

    Is there a way to call a printDialog from a printPreviewDia log (from print
    icon for example)?
    Indeed, the print icon in the printpreviewdia log prints directly the
    document without lunching the printdialog (i.e. the user can't choose the
    print parameters).

    Thanks a lot.


  • Bruce Wood

    #2
    Re: Access to a printDialog from a PrintPreviewDia log


    choupi wrote:
    Hello,
    >
    Is there a way to call a printDialog from a printPreviewDia log (from print
    icon for example)?
    Indeed, the print icon in the printpreviewdia log prints directly the
    document without lunching the printdialog (i.e. the user can't choose the
    print parameters).
    >
    Thanks a lot.
    I ended up creating a File --Print... menu option that brings up the
    print dialog.

    One of the irritating things about the print preview dialog is that a
    lot of things that you would want to change (like this particular
    point) are "private" within the control and therefore not changeable. I
    would have liked to have seen protected methods like
    OnPrintButtonCl ick, etc, so that one could override the behaviour.

    It would also have been nice to be able to hide / disable buttons on
    the toolbar if we wanted to, even if the only way were to set a
    PrintButtonEnab led property or something like that.

    PrintPreviewDia log and TabControl are two of the hokier controls in
    ..NET. I hope that someday they'll revisit them and make much-needed
    improvements.

    Comment

    • choupi

      #3
      Re: Access to a printDialog from a PrintPreviewDia log

      Ok, so there's no way to add a new button in the toolbar I imagine ?!

      "Bruce Wood" <brucewood@cana da.coma écrit dans le message de news:
      1164929060.6693 27.63600@h54g20 00...legro ups.com...
      >
      choupi wrote:
      >Hello,
      >>
      >Is there a way to call a printDialog from a printPreviewDia log (from
      >print
      >icon for example)?
      >Indeed, the print icon in the printpreviewdia log prints directly the
      >document without lunching the printdialog (i.e. the user can't choose the
      >print parameters).
      >>
      >Thanks a lot.
      >
      I ended up creating a File --Print... menu option that brings up the
      print dialog.
      >
      One of the irritating things about the print preview dialog is that a
      lot of things that you would want to change (like this particular
      point) are "private" within the control and therefore not changeable. I
      would have liked to have seen protected methods like
      OnPrintButtonCl ick, etc, so that one could override the behaviour.
      >
      It would also have been nice to be able to hide / disable buttons on
      the toolbar if we wanted to, even if the only way were to set a
      PrintButtonEnab led property or something like that.
      >
      PrintPreviewDia log and TabControl are two of the hokier controls in
      .NET. I hope that someday they'll revisit them and make much-needed
      improvements.
      >

      Comment

      • choupi

        #4
        Re: Access to a printDialog from a PrintPreviewDia log

        Well, I just have found a simple way using Reflection, and it works
        perfectly.
        Let me know if you're interested.

        Thanks.


        Comment

        • RobinS

          #5
          Re: Access to a printDialog from a PrintPreviewDia log

          We're interested!
          Robin S.
          ---------------------------------
          "choupi" <none@none.comw rote in message
          news:%23vXmLjSF HHA.3288@TK2MSF TNGP05.phx.gbl. ..
          Well, I just have found a simple way using Reflection, and it works
          perfectly.
          Let me know if you're interested.
          >
          Thanks.
          >

          Comment

          • choupi

            #6
            Re: Access to a printDialog from a PrintPreviewDia log

            Code:
            using System;
            
            using System.Reflection;
            
            using System.Drawing;
            
            using System.Drawing.Printing;
            
            using System.Windows.Forms;
            
            namespace Test
            
            {
            
            public class MyPrintPreviewDialog : System.Windows.Forms.PrintPreviewDialog
            
            {
            
            private ToolBarButton myPrintButton;
            
            public MyPrintPreviewDialog() : base()
            
            {
            
            Type t = typeof(PrintPreviewDialog);
            
            FieldInfo fi = t.GetField("toolBar1", BindingFlags.Instance |
            BindingFlags.NonPublic);
            
            FieldInfo fi2 = t.GetField("printButton", BindingFlags.Instance |
            BindingFlags.NonPublic);
            
            ToolBar toolBar1 = (ToolBar)fi.GetValue(this);
            
            ToolBarButton printButton = (ToolBarButton)fi2.GetValue(this);
            
            
            printButton.Visible = false;
            
            
            myPrintButton = new ToolBarButton();
            
            myPrintButton.ToolTipText = printButton.ToolTipText;
            
            myPrintButton.ImageIndex = 0;
            
            
            ToolBarButton[] oldButtons = new ToolBarButton[toolBar1.Buttons.Count-1];
            
            for(int i = 0 ; i < oldButtons.Length ; i++) oldButtons[i] =
            toolBar1.Buttons[i+1];
            
            
            toolBar1.Buttons.Clear();
            
            toolBar1.Buttons.Add(myPrintButton);
            
            for(int i = 0 ; i < oldButtons.Length ; i++)
            toolBar1.Buttons.Add(oldButtons[i]);
            
            toolBar1.ButtonClick += new ToolBarButtonClickEventHandler(toolBar1_Click);
            
            }
            
            private void toolBar1_Click(object sender, ToolBarButtonClickEventArgs
            eventargs)
            
            {
            
            if (eventargs.Button == myPrintButton)
            
            {
            
            PrintDialog printDialog1 = new PrintDialog();
            
            printDialog1.Document = this.Document;
            
            if (printDialog1.ShowDialog() == DialogResult.OK) this.Document.Print();
            
            }
            
            }
            
            }
            
            }
            Last edited by tlhintoq; Aug 26 '09, 04:00 PM. Reason: [CODE] ...your code goes here... [/CODE] tags added

            Comment

            • RobinS

              #7
              Re: Access to a printDialog from a PrintPreviewDia log

              Thanks!
              Robin S.
              "choupi" <none@none.comw rote in message
              news:uWyFMTIGHH A.3952@TK2MSFTN GP02.phx.gbl...[QUOTE]

              Comment

              • Duggi

                #8
                Re: Access to a printDialog from a PrintPreviewDia log

                thats quite interesting!!!

                Thanks
                -Srinivas.

                choupi wrote:
                Hello,
                >
                Is there a way to call a printDialog from a printPreviewDia log (from print
                icon for example)?
                Indeed, the print icon in the printpreviewdia log prints directly the
                document without lunching the printdialog (i.e. the user can't choose the
                print parameters).
                >
                Thanks a lot.

                Comment

                • davidjt52

                  #9
                  Re: Access to a printDialog from a PrintPreviewDia log

                  Thanks for the starting place for the code needed to add the
                  PrintDialog to PrintPreviewDia log. However, I came across a number of
                  issues when I tried to implement your code in VS2005 (did you write
                  your version under VS2003? Just curious...). Here is code that works
                  under VS2005:

                  Code:
                  public class MyPrintPreviewDialog :
                  System.Windows.Forms.PrintPreviewDialog
                  {
                  private ToolStripButton myPrintButton;
                  public MyPrintPreviewDialog ( ) : base ( )
                  {
                  Type t = typeof ( PrintPreviewDialog );
                  FieldInfo fi = t.GetField ( "toolStrip1",
                  BindingFlags.Instance | BindingFlags.NonPublic );
                  FieldInfo fi2 = t.GetField ( "printToolStripButton",
                  BindingFlags.Instance | BindingFlags.NonPublic );
                  ToolStrip toolStrip1 =
                  ( ToolStrip ) fi.GetValue ( this );
                  ToolStripButton printButton =
                  ( ToolStripButton ) fi2.GetValue ( this );
                  printButton.Visible = false;
                  myPrintButton = new ToolStripButton ( );
                  myPrintButton.ToolTipText = printButton.ToolTipText;
                  myPrintButton.ImageIndex = 0;
                  
                  ToolStripItem [ ] oldButtons =
                  new ToolStripItem [ toolStrip1.Items.Count ];
                  for ( int i = 0; i < oldButtons.Length; i++ )
                  oldButtons [ i ] = toolStrip1.Items [ i ];
                  
                  toolStrip1.Items.Clear ( );
                  toolStrip1.Items.Add ( myPrintButton );
                  for ( int i = 0; i < oldButtons.Length; i++ )
                  toolStrip1.Items.Add ( oldButtons [ i ] );
                  toolStrip1.ItemClicked +=
                  new ToolStripItemClickedEventHandler ( toolBar1_Click );
                  }
                  
                  private void toolBar1_Click ( object sender,
                  ToolStripItemClickedEventArgs eventargs )
                  {
                  if ( eventargs.ClickedItem == myPrintButton )
                  {
                  PrintDialog printDialog1 = new PrintDialog ( );
                  printDialog1.Document = this.Document;
                  if ( printDialog1.ShowDialog ( ) == DialogResult.OK )
                  this.Document.Print ( );
                  }
                  }
                  }

                  Thanks again for your inspiration.

                  Comment

                  • yannick.devisscher@gmail.com

                    #10
                    Re: Access to a printDialog from a PrintPreviewDia log

                    Thx!! this was very useful !

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

                      Comment

                      Working...