Howto use PrintPreviewControl

I have an application that prints invoices but before printing the user should be able to print preview each invoice in a printpreviewcontrol.
I have a printpreviewcontrol and a printdocument control on my form. I set the document of the printpreviewcontrol to printdocument1. When I call printdocument1.print function it prints on a file not on the preview control. How can I make it to print on the preview control?



ppc_invoice.Document = PrintDocument1
PrintDocument.Print

Open in new window

emresamisuzerAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
Try creating another test form, and see how the controls work together, to make sure that the DockPanel is what is the culprit.
0
 
Bob LearnedCommented:
See if this article helps...

Print Dialogs  
http://www.startvbdotnet.com/controls/printdialog.aspx

Hint:  The Print method prints--period.
0
 
emresamisuzerAuthor Commented:
It writes the following code but it doesn't work for me.

Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem5.Click
Try
PrintPreviewControl1.Document = PrintDocument1
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Bob LearnedCommented:
How are you creating the PrintDocument?  Are you handing the PrintPage event?
0
 
emresamisuzerAuthor Commented:
The printdocument is created at design time (dragged from the toolbox). It has the printpage event and if I call printdocument1.printpage it prints to a paper.
0
 
Bob LearnedCommented:
Ok, what does the PrintDocument_PrintPage event handler look like?  I am not suggesting using the Print method, since I told you that will send the output to the printer.
0
 
emresamisuzerAuthor Commented:
The fonts and locations are from a dataset, but the printpage events is not triggered.
 e.Graphics.DrawString("TEST", fonts(dr("id_font")), Drawing.Brushes.Black, CInt(dr("x")), CInt(dr("y")))

Open in new window

0
 
emresamisuzerAuthor Commented:
Sorry, you asked the handler
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

Open in new window

0
 
Bob LearnedCommented:
If you drop a PrintDocument and PrintPreviewControl on a form, and set the PrintPreviewControl.Document property, and you have code in the PrintPage event handler, you should get a document on the PrintPreviewControl.

Example:


Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
  e.Graphics.DrawString("This is a test", Me.Font, Brushes.Red, New PointF(50, 50))
End Sub

Open in new window

0
 
emresamisuzerAuthor Commented:
I am using WeifenLuo dockpanel, I think it somehow blocks the print_page event?
0
 
emresamisuzerAuthor Commented:
Yes, the dockpanel is the reason. Thanks.
0
All Courses

From novice to tech pro — start learning today.