Access to a printDialog from a PrintPreviewDialog

C

choupi

Hello,

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

Thanks a lot.
 
B

Bruce Wood

choupi said:
Hello,

Is there a way to call a printDialog from a printPreviewDialog (from print
icon for example)?
Indeed, the print icon in the printpreviewdialog 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
OnPrintButtonClick, 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
PrintButtonEnabled property or something like that.

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

choupi

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

Thanks.
 
C

choupi

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 =
toolBar1.Buttons[i+1];


toolBar1.Buttons.Clear();

toolBar1.Buttons.Add(myPrintButton);

for(int i = 0 ; i < oldButtons.Length ; i++)
toolBar1.Buttons.Add(oldButtons);

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();

}

}

}

}
 
D

davidjt52

Thanks for the starting place for the code needed to add the
PrintDialog to PrintPreviewDialog. 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:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top