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.
 
Sorry, I try this...
VB.NET:
WithEvents tb As ToolStripItem 
    Private tbt As ToolStripButton     
Private preview As Boolean = False

But always get error...
 
try this:
WithEvents printItem As ToolStripItem

Assign the print toolstrip item to the printItem variable.
Handle the Click and MouseDown events for it.
 
Ok. I try this but always gets error when click on printButton.
"Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.ToolBarButtonClickEventArgs'."

Sorry, I'm never try DirectCast or similar modification like this.

WithEvents printItem As ToolStripItem
Private preview As Boolean = False

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
printItem = DirectCast(DirectCast(PrintPreviewDialog1.Controls(1), ToolStrip).Items(0), ToolStripButton)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog() //there is error
End Sub

Private Sub printItem_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles printItem.MouseDown
preview = True
End Sub

Private Sub printItem_ButtonClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles printItem.Click
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
 
Private Sub printItem_ButtonClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles printItem.Click
The signature for your event handler is not correct. Remove this and add the event handler for the Click event of the printItem Withevents variable. To do this select from top of code window the object (printItem) in left combo, and the event (Click) in right combo.
 
To do this select from top of code window the object (printItem) in left combo, and the event (Click) in right combo.
I do not understand this...

I also try with this
Private Sub printItem_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles printItem.Click
but the same error occured.
 
To do this select from top of code window the object (printItem) in left combo, and the event (Click) in right combo.
I do not understand this...
You know the code window, right? At the top there are two combo boxes that you need to use.
 
Back
Top