i need to print my vb.net windows form when the user click a button. what is the method for this?
+5
A:
You need to use the PrintForm
component. Please see How to: Print a Form by Using the PrintForm Component:
The PrintForm component enables you to quickly print an image of a form exactly as it appears on screen without using a PrintDocument component. The following procedures show how to print a form to a printer, to a print preview window, and to an Encapsulated PostScript file.
Andrew Hare
2009-06-08 19:42:24
A:
If you really want a snapshot of the form, you'll have to simulate a screen capture. Here's a sample that'll help. It's going to be pretty ugly though, so you may want to consider making it a report using a PDF or report builder.
jvenema
2009-06-08 19:42:57
Theres a new component.
Daniel A. White
2009-06-08 19:43:31
Huh, had no idea. Cool, tnx. :)
jvenema
2009-06-08 19:44:40
+1
A:
If you want to amend the image the following code will capture the bitmap and send it to the printer (On the Button1 Click)
Imports System.Drawing.Printing
Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
' Draw the image centered.
Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2
e.Graphics.DrawImage(mPrintBitMap, lWidth, lheight)
' There's only one page.
e.HasMorePages = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Copy the form's image into a bitmap.
mPrintBitMap = New Bitmap(Me.Width, Me.Width)
Dim lRect As System.Drawing.Rectangle
lRect.Width = Me.Width
lRect.Height = Me.Width
Me.DrawToBitmap(mPrintBitMap, lRect)
' Make a PrintDocument and print.
mPrintDocument = New PrintDocument
mPrintDocument.Print()
End Sub
End Class
spacemonkeys
2009-06-08 20:56:11