print dialog after press print button in printpreview dialog?

milosh

Active member
Joined
May 11, 2011
Messages
33
Programming Experience
1-3
Hi all.
How can I achieve this?
I need to show print dialog after pressing print button on print preview dialog.
Thanks.
 
Thanks for replies...
I see the link above (first reply), but when I try to execute the code error showing.
It's error about casting
Unable to cast object of type 'System.Windows.Forms.ToolStrip' to type 'System.Windows.Forms.ToolBar'.
in line tb = DirectCast(PrintPreviewDialog1.Controls(1), ToolBar)

vb.net express 2008, .net framework 3.5

How to fix this?

Second reply, how to include this control to existing project?
 
Last edited:
Read post 3 in that thread. Post 2 from year 2006 was only relevant for .Net 1.
 
Thanks John.
But reply number 3 is for disabling print button?
I need functionality to open print dialog after click on print button on print preview dialog.
 
I would say the explanation and code is relevant for your platform though.
Anyway, did you try the Code Project dialog I linked to?
 
I would say the explanation and code is relevant for your platform though.
Anyway, did you try the Code Project dialog I linked to?

I download source and try that...it works.
But how to include that printdialog in existing project?
 
You can add the threee .vb files to your project (CoolPrintPreviewControl.vb, CoolPrintPreviewDialog.vb, PageImageList.vb) using the Add Existing Item dialog.
 
But, do you know how to implement solution explained before:

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) // there is cast error
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

Thanks a lot for help!!!
 
there is cast error
That one you can fix by reading the code Juggalobrotha posted.
 
there is cast error
That one you can fix by reading the code Juggalobrotha posted.
 
I tried with this, but again have error:

VB.NET:
WithEvents [B]tb As ToolStrip
    Private tbt As ToolStripButton[/B]
    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)
       [B] tb = DirectCast(PrintPreviewDialog1.Controls(1), ToolStrip)
        tbt = DirectCast(DirectCast(PrintPreviewDialog1.Controls(1), ToolStrip).Items(0), ToolStripButton)[/B]
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintPreviewDialog1.ShowDialog() //[B]there is error when click on print button[/B] [B]: Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.ToolBarButtonClickEventArgs'.[/B]
    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 [B]tb.Click[/B]
        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

End Class
 
You can forget the ToolStrip and WithEvents the ToolStripItem instead, and handle its MouseDown and Click events similarily.
 
Back
Top