Printing under VB.NET is completely different to VB6.
You'll need a day or two to learn the new way of doing things. It's more
complicated but gives you better control of the printer and allows print
preview very easily.
If you want a quick fix, use this object:
''' <summary>
''' Bare bones printout
''' </summary>
''' <remarks></remarks>
Public Class SimplePrintout
Public Sub Print(Optional ByVal PrinterName As String = "")
'create the document object
Using pdcNew As New Printing.PrintDocument
'wire up event handlers to handle pagination
AddHandler pdcNew.PrintPage, AddressOf PrintPage
Using docOutput As Printing.PrintDocument = pdcNew
If PrinterName > "" Then
docOutput.PrinterSettings.PrinterName = PrinterName
End If
docOutput.Print()
End Using
End Using
End Sub
''' <summary>
''' Preview the Report on screen
''' </summary>
''' <remarks></remarks>
Public Sub PrintPreview(Optional ByVal Owner As Form = Nothing)
'create the document object
Using pdcNew As New Printing.PrintDocument
'wire up event handlers to handle pagination
AddHandler pdcNew.PrintPage, AddressOf PrintPage
Using ppvPreview As New PrintPreviewDialog
ppvPreview.Document = pdcNew
ppvPreview.FindForm.WindowState = FormWindowState.Maximized
If IsNothing(Owner) Then
ppvPreview.ShowDialog()
Else
ppvPreview.ShowDialog(Owner)
End If
End Using
End Using
End Sub
Sub PrintPage(ByVal sender As System.Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs)
Dim g As Graphics = e.Graphics 'shortcut
Dim x As Single = e.MarginBounds.Left '"Cursor" location
Dim y As Single = e.MarginBounds.Top '"Cursor" location
'g.DrawRectangle(Pens.Black, e.MarginBounds) '>>DEBUG: use this line
to check margins
Dim fnt1 As New Font(System.Drawing.FontFamily.GenericSansSerif, 12,
FontStyle.Regular, GraphicsUnit.Point)
g.DrawString("Simple printout line 1" & vbCrLf & " after CRLF",
fnt1, Brushes.Black, x, y)
y += fnt1.GetHeight(g)
g.DrawString("Simple printout line 2", fnt1, Brushes.Black, x, y)
y += fnt1.GetHeight(g)
g.DrawString("Simple printout line 3", fnt1, Brushes.Black, x, y)
y += fnt1.GetHeight(g)
e.HasMorePages = False
End Sub
End Class
Put your printing code in PrintPage(), then in your command button (or
whatever):
Dim p As New SimplePrintout
p.PrintPreview