PrintPreviewDialog & PrintDialog

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
this is for vb2003

i was wondering if there was a way that in the PrintPreviewDialog if the user clicks the print button (top left button) it opens the PrintDialog so the user can select the printer

by default the PrintPreview uses the default printer in windows
 
Here is how to do it, it is quite a hack.. (perhaps better creating your own print preview dialog with the printpreviewcontrol?)
In application project there is a printdocument, a printdialog and a printpreviewdialog, the printdocument is added to both dialogs document property.
Printpreviewdialog is started with a button in main form for this example.
I found a note somewhere saying one of the controls in previewdialog was a toolbar, I get this to withevents variable and the print toolbarbutton to global variable in form_load.
Event handlers for toolbar buttonclick and mousedown is setup.
For printdocument_beginprint I have a boolean to escape printing "when I say so", in printdocument_printpage I just draw a black square.
Toolbar mousedown is where I escape printing by setting the boolean, I have found no other way to stop the default behaviour of clicking the print button in printpreviewdialog. (my own buttonclick handler is triggered after the default handler in the dialog)
Next thing that happens is buttonclick in toolbar, where the boolean is changed back to again enable printing, I check if its the print button (tbt), if so display printdialog, if 'ok' is pressed there then print document.
That's it, perhaps harder to describe than to code.. :)
VB.NET:
WithEvents tb As ToolBar
Private tbt As ToolBarButton
Private preview As Boolean = False
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  tb = DirectCast(PrintPreviewDialog1.Controls(1), ToolBar)
  tbt = tb.Buttons(0)
End Sub
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  PrintPreviewDialog1.ShowDialog()
End Sub
 
Private Sub tb_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tb.MouseDown
  preview = True
End Sub
 
Private Sub tb_ButtonClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles tb.ButtonClick
  preview = False
  If e.Button Is tbt Then  
    If PrintDialog1.ShowDialog() = DialogResult.OK Then PrintDocument1.Print()
  End If
End Sub
 
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
  If preview = True Then e.Cancel = True
End Sub
 
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  e.Graphics.FillRectangle(Brushes.Black, 100, 100, 100, 100)
End Sub
 
In VS 2005 and VS 2008 to simply disable the Print button itself, use this code:
VB.NET:
        Dim ppd As New PrintPreviewDialog
        DirectCast(DirectCast(ppd.Controls(1), ToolStrip).Items(0), ToolStripButton).Enabled = False
        ppd.ShowDialog()
ppd is simply the dialog, it can be a control on your form. Here I used ShowDialog() but you can use Show() for a non-modal window.

Also, all of this code is Option Strict compliant
 
Back
Top