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