Reputation: 11
I created one windows application using VS2013, the form contains few labels and textboxes. Form1
has little bit larges so I unable to print in actual size, it prints on portrait mode.
In my project I have added PrintForm
and PageSetup
dialogue, but this page setup won't works well, if I click on Landscape in PageSetup
and then print, it prints the form in portrait mode.
page setup coding
' initialize the page settings
PageSetupDialog1.PageSettings = New Printing.PageSettings
' hide the network button
PageSetupDialog1.ShowNetwork = False
If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim settings() As Object = New Object() _
{PageSetupDialog1.PageSettings.Margins, _
PageSetupDialog1.PageSettings.PaperSize, _
PageSetupDialog1.PageSettings.Landscape, _
PageSetupDialog1.PrinterSettings.PrinterName, _
PageSetupDialog1.PrinterSettings.PrintRange}
End If
Upvotes: 0
Views: 4831
Reputation: 87
You need to think about if you really want to print the whole form as there might be buttons etc the user doesn't need to see and it is worthwhile learning how to design a page to be printed only containing relevant information.
For printing a form you can:
Option 1) Download the Visual Basic Powerpack as it contains the form print control and use this:
or
Option 2) Turn your form into a bitmap and put into the document.print routine. Here is some code you can play around with:
Imports System.Drawing.Printing
Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim prtdoc As New PrintDocument
Dim strDefaultPrinter As String = prtdoc.PrinterSettings.PrinterName
MsgBox(strDefaultPrinter)
End Sub
Private Sub Button1_Click(sender As Object, e As 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.DefaultPageSettings.Landscape = True
'mPrintDocument.Print() 'send the document to the printer
Me.PrintPreviewDialog1.Document = mPrintDocument
PrintPreviewDialog1.ShowDialog()
End Sub
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 Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
End Class
Upvotes: 1