Click to See Complete Forum and Search --> : Simple printing with VB.NET


Dave C
May 1st, 2003, 07:27 AM
I need to print some text with a network printer from my VB.NET application. All the Printer methods from VB6 are gone and instead we have this incredibly complex PrintDocument control.

So far I have put a PrintDialog control and PrintDocument Control on the form. The user can select which printer to use, etc with the PrintDialog. One of the properties is the PrintDocument object. After that, I'm lost.

Examples I've seen so far are extremely complicated. Can someone please direct me to a tutorial or example of using this control to do some simple formatted text?

Or maybe a snippet of code here? Just to print "Hello World"?

Thanks.

DSJ
May 1st, 2003, 09:56 AM
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintDocument1.Print()
End Sub


Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim _DocFont As Font = New Font("Arial", 10, GraphicsUnit.Point)
Dim _Brush As Brush = Brushes.Black
Dim _X As Single
Dim _Y As Single
Dim s As String

e.Graphics.PageUnit = GraphicsUnit.Inch
s = "Hello there"
'Set X and Y Coordinate of where to print
_X = (e.MarginBounds.Left / e.Graphics.DpiX) + 0.5
_Y = (e.MarginBounds.Top / e.Graphics.DpiY) + 0.5
e.Graphics.DrawString(s, _DocFont, _Brush, _X, _Y)

_Y += (e.Graphics.MeasureString(s, _DocFont).Height)
s = "This is line 2"
e.Graphics.DrawString(s, _DocFont, _Brush, _X, _Y)

'Move down 5 inches
_Y += 5
'Move over 2 inches
_X += 2
s = "This line is down 5 inches and over 2"
e.Graphics.DrawString(s, _DocFont, _Brush, _X, _Y)

'Return False when there are no more pages....
e.HasMorePages = False
End Sub

Dave C
May 1st, 2003, 12:32 PM
Outstanding. Thank you very much.