Printing using PrintPreview Print Button

I am getting Exception if i try to use the Print Built-in Button within PrintPreviewDialog.

In my sample, a can have a multi-page preview but i can't print it !

There must be a stupid mistake (my mistake, of course).

There must be someone that can help with this. I have used F8 to watch code execution and when

e.Graphics.DrawString("This is number " + Format(N,"###"), _P_Font, Brushes.Black, 10, Yposition + 5)

is fired, after i click the built-in Print Button,TestMultiPagePreviewPrint_EndPrint is also fired and ...

... and nothing ! There must be something lefting, maybe ! I don't know !

Thank's in advance

Used code to improve this:


Imports System

Imports System.IO

Imports System.Drawing

Imports System.Drawing.Printing

Imports System.ComponentModel


PublicClass Form1

Inherits System.Windows.Forms.Form

PrivateWithEvents TestMultiPagePreviewPrintAs PrintDocument

Private _currentPageAsInteger = 0

Private _P_FontAs Font

Private _P_BrushAs Brush = Brushes.Black

Private _P_ThicknessAsNew Pen(Brushes.Black, 0.25F)

Private FirstPoint, LastPointAsInteger


PrivateSub TestMultiPagePreviewPrint_BeginPrint(ByVal senderAsObject,ByVal eAs System.Drawing.Printing.PrintEventArgs)Handles TestMultiPagePreviewPrint.BeginPrint

_P_Font =New Font("Arial", 8, FontStyle.Regular, GraphicsUnit.Point)

EndSub


PrivateSub TestMultiPagePreviewPrint_EndPrint(ByVal senderAsObject,ByVal eAs System.Drawing.Printing.PrintEventArgs)Handles TestMultiPagePreviewPrint.EndPrint

_P_Brush.Dispose()

_P_Font.Dispose()

_P_Thickness.Dispose()

EndSub


PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click

Try

Dim previewDialogAsNew PrintPreviewDialog

TestMultiPagePreviewPrint =New PrintDocument

TestMultiPagePreviewPrint.DocumentName ="Testing Multi-page printing with PrintPreview"

previewDialog.PrintPreviewControl.Zoom = 1.0

previewDialog.WindowState = FormWindowState.Maximized

previewDialog.ShowInTaskbar =True

'\\to test multi-page

FirstPoint = 1

LastPoint = 200

previewDialog.Document = TestMultiPagePreviewPrint

previewDialog.ShowDialog()

Catch exAs Exception

MsgBox(ex.Message & vbCrLf & ex.StackTrace)

EndTry

EndSub


PrivateSub TestMultiPagePreviewPrint_PrintPage(ByVal senderAsObject,ByVal eAs System.Drawing.Printing.PrintPageEventArgs)Handles TestMultiPagePreviewPrint.PrintPage

Dim NAsInteger

Dim YpositionAsInteger

e.Graphics.PageUnit = GraphicsUnit.Millimeter

Yposition = 0

If FirstPoint > LastPointThen

e.HasMorePages =False

ExitSub

EndIf

For N = FirstPointTo LastPoint

Yposition += 5

e.Graphics.DrawString("This is number " + Format(N,"###"), _P_Font, Brushes.Black, 10, Yposition + 5)

If Yposition > 260Then

e.HasMorePages =True

FirstPoint = N + 1

ExitSub

Else

e.HasMorePages =False

EndIf

Next

e.HasMorePages =False

EndSub

EndClass

[9917 byte] By [AbílioRodrigues] at [2007-12-28]
# 1

Your printpage code is used to generate the printpreview.

The same code is called (using a different print controller,)

when the print button is pressed.

Most of you 'counter' stuff needs to be re-set to it's startng

value. Also don't dipose on the endprint event at all.

(I added a 'do while' loop while playing with the code that may

not be needed.)

'Private Sub TestMultiPagePreviewPrint_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles TestMultiPagePreviewPrint.EndPrint

' _P_Brush.Dispose()

' _P_Font.Dispose()

' _P_Thickness.Dispose()

'End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

Dim previewDialog As New PrintPreviewDialog

TestMultiPagePreviewPrint = New PrintDocument

TestMultiPagePreviewPrint.DocumentName = "Testing Multi-page printing with PrintPreview"

previewDialog.PrintPreviewControl.Zoom = 1.0

previewDialog.WindowState = FormWindowState.Maximized

previewDialog.ShowInTaskbar = True

'\\to test multi-page

FirstPoint = 1

LastPoint = 200

previewDialog.Document = TestMultiPagePreviewPrint

previewDialog.ShowDialog()

Catch ex As Exception

MsgBox(ex.Message & vbCrLf & ex.StackTrace)

End Try

End Sub

Private Sub TestMultiPagePreviewPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles TestMultiPagePreviewPrint.PrintPage

Dim t As Boolean = True

Do While t

Dim N As Integer

Dim Yposition As Integer

e.Graphics.PageUnit = GraphicsUnit.Millimeter

Yposition = 0

If FirstPoint > LastPoint Then

e.HasMorePages = False

FirstPoint = 1

LastPoint = 200

Exit Do

End If

For N = FirstPoint To LastPoint

Yposition += 5

e.Graphics.DrawString("This is number " + Format(N, "###"), _P_Font, Brushes.Black, 10, Yposition + 5)

If Yposition > 260 Then

e.HasMorePages = True

FirstPoint = N + 1

Exit Do

Else

e.HasMorePages = False

FirstPoint = 1

LastPoint = 200

End If

Next

e.HasMorePages = False

FirstPoint = 1

LastPoint = 200

t = False

Loop

End Sub

End Class

TallDude at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Thank's Tall Dude !

It's working now !

AbílioRodrigues at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...