PrintPreview problem

C

Chuck Gantz

I'm using vb2005.

I wrote a simple app to view and print all the fonts on my pc.To see how the
printout will look without wasting lots of paper, I use PrintPreviewDialog.

The first time I run PrintPreviewDialog, it correctly shows five pages of
printout. If I run it a second time, it only shows three pages, with the
original second page printing over the original first page.
If I run it a third time, only one page shows up, with all the fonts being
printed on that page.

Can anyone see what's wrong. Thanks.
p.s., I'm not sure why when I copy my code all my formatting dissapeared.

'First class
Imports System.Drawing.Printing
Public Class FontDocument
Inherits printdocument
Private m_fontFamilies() As FontFamily
Private m_pageNumber As Int32
Private m_offset As Int32
Public Property FontFamilies() As FontFamily()
Get
Return m_fontFamilies
End Get
Set(ByVal value As FontFamily())
m_fontFamilies = value
End Set
End Property
Public Property PageNumber() As Int32
Get
Return m_pageNumber
End Get
Set(ByVal value As Int32)
m_pageNumber = value
End Set
End Property
Public Property Offset() As Int32
Get
Return m_offset
End Get
Set(ByVal value As Int32)
m_offset = value
End Set
End Property
Public Sub FontDocument(ByVal fontFamilies() As FontFamily)
Me.m_fontFamilies = fontFamilies
End Sub
End Class

'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PrintDocument1 = New FontDocument
PrintDocument1.FontFamilies = FontFamily.Families
nudFontSize.Value = 10
LoadFontsRichTextBox()
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PrintPreviewToolStripMenuItem.Click
AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
Dim PrintPreviewDialog1 As PrintPreviewDialog = New
System.Windows.Forms.PrintPreviewDialog
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.PageNumber = 0
PrintDocument1.Offset = 0
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal ev
As System.Drawing.Printing.PrintPageEventArgs)
Dim fnt As Font
Dim linesPerPage As Integer = 0
Dim yPos As Integer = ev.MarginBounds.Top
Dim count As Integer = 0
Dim leftMargin As Integer = ev.MarginBounds.Left
Dim topMargin As Integer = ev.MarginBounds.Top
Dim line As String = Nothing
Dim fntFamily As FontFamily
If sender Is Nothing OrElse ev Is Nothing Then
Return
End If
Dim fntDoc As FontDocument = CType(sender, FontDocument)
fntDoc.PageNumber += 1
While yPos < ev.MarginBounds.Bottom And fntDoc.Offset <
fntDoc.FontFamilies.Length
fntFamily = fntDoc.FontFamilies(fntDoc.Offset)
Try
fnt = New Font(fntFamily, nudFontSize.Value, FontStyle.Regular)
ev.Graphics.DrawString(fntFamily.Name, fnt, Brushes.Black, 150, yPos)
yPos += fnt.Height

If yPos > ev.MarginBounds.Bottom Then
ev.HasMorePages = True
Return
Else
ev.HasMorePages = False
End If
Catch ex As Exception
Finally
fntDoc.Offset += 1
End Try
End While
ev.HasMorePages = False
End Sub
 
T

tommaso.gastaldi

Hi Chuck. At first sight, it would seem a good idea to add

yPos = ev.MarginBounds.Top
fntDoc.Offset = 0
fntDoc.PageNumber = 0

[and any other var needing reset]

after

Dim fntDoc As FontDocument = CType(sender, FontDocument)

(where you could actually use a DirectCast instead of ctype. You migh
want to to place a MsgBox(ex.Message, MsgBoxStyle.Information) in
the catch clause)....


-tom

Chuck Gantz ha scritto:
 
C

Chuck Gantz

Hi Tom,

Thanks for the reply.

The lines
yPos = ev.MarginBounds.Top
fntDoc.Offset = 0
fntDoc.PageNumber = 0

actually are in the program. (It would be nice if the formatting would have
copied over so that my code could be read a little easier). The offset and
PageNumber variables can't be set in PrintDocument1_PrintPage since they
change for each line of text and page.

Also, I don't use a messageBox in the catch clause because the code doesn't
reach there for an actual error. Some font families don't have a regular
font style. When that happens the line
fnt = New Font(fntFamily, nudFontSize.Value, FontStyle.Regular)

throws an exception. The catch clause keeps the program from ending because
of that exception.

Chuck

Hi Chuck. At first sight, it would seem a good idea to add

yPos = ev.MarginBounds.Top
fntDoc.Offset = 0
fntDoc.PageNumber = 0

[and any other var needing reset]

after

Dim fntDoc As FontDocument = CType(sender, FontDocument)

(where you could actually use a DirectCast instead of ctype. You migh
want to to place a MsgBox(ex.Message, MsgBoxStyle.Information) in
the catch clause)....


-tom

Chuck Gantz ha scritto:
I'm using vb2005.

I wrote a simple app to view and print all the fonts on my pc.To see how
the
printout will look without wasting lots of paper, I use
PrintPreviewDialog.

The first time I run PrintPreviewDialog, it correctly shows five pages of
printout. If I run it a second time, it only shows three pages, with the
original second page printing over the original first page.
If I run it a third time, only one page shows up, with all the fonts
being
printed on that page.

Can anyone see what's wrong. Thanks.
p.s., I'm not sure why when I copy my code all my formatting dissapeared.

'First class
Imports System.Drawing.Printing
Public Class FontDocument
Inherits printdocument
Private m_fontFamilies() As FontFamily
Private m_pageNumber As Int32
Private m_offset As Int32
Public Property FontFamilies() As FontFamily()
Get
Return m_fontFamilies
End Get
Set(ByVal value As FontFamily())
m_fontFamilies = value
End Set
End Property
Public Property PageNumber() As Int32
Get
Return m_pageNumber
End Get
Set(ByVal value As Int32)
m_pageNumber = value
End Set
End Property
Public Property Offset() As Int32
Get
Return m_offset
End Get
Set(ByVal value As Int32)
m_offset = value
End Set
End Property
Public Sub FontDocument(ByVal fontFamilies() As FontFamily)
Me.m_fontFamilies = fontFamilies
End Sub
End Class

'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PrintDocument1 = New FontDocument
PrintDocument1.FontFamilies = FontFamily.Families
nudFontSize.Value = 10
LoadFontsRichTextBox()
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PrintPreviewToolStripMenuItem.Click
AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
Dim PrintPreviewDialog1 As PrintPreviewDialog = New
System.Windows.Forms.PrintPreviewDialog
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.PageNumber = 0
PrintDocument1.Offset = 0
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal
ev
As System.Drawing.Printing.PrintPageEventArgs)
Dim fnt As Font
Dim linesPerPage As Integer = 0
Dim yPos As Integer = ev.MarginBounds.Top
Dim count As Integer = 0
Dim leftMargin As Integer = ev.MarginBounds.Left
Dim topMargin As Integer = ev.MarginBounds.Top
Dim line As String = Nothing
Dim fntFamily As FontFamily
If sender Is Nothing OrElse ev Is Nothing Then
Return
End If
Dim fntDoc As FontDocument = CType(sender, FontDocument)
fntDoc.PageNumber += 1
While yPos < ev.MarginBounds.Bottom And fntDoc.Offset <
fntDoc.FontFamilies.Length
fntFamily = fntDoc.FontFamilies(fntDoc.Offset)
Try
fnt = New Font(fntFamily, nudFontSize.Value, FontStyle.Regular)
ev.Graphics.DrawString(fntFamily.Name, fnt, Brushes.Black, 150, yPos)
yPos += fnt.Height

If yPos > ev.MarginBounds.Bottom Then
ev.HasMorePages = True
Return
Else
ev.HasMorePages = False
End If
Catch ex As Exception
Finally
fntDoc.Offset += 1
End Try
End While
ev.HasMorePages = False
End Sub
 
T

tommaso.gastaldi

Yes it's actually uncomfortable to read, but gets better in the editor
:) I have good new. I think I located your problem. You are attacching
multiple handlers ...

Replace this part:

'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
PrintDocument1 = New FontDocument
AddHandler PrintDocument1.PrintPage, AddressOf
PrintDocument1_PrintPage
PrintDocument1.FontFamilies = FontFamily.Families
nudFontSize.Value = 10
LoadFontsRichTextBox()
End Sub

Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles Button1.Click '
PrintPreviewToolStripMenuItem.Click
Dim PrintPreviewDialog1 As PrintPreviewDialog = New
System.Windows.Forms.PrintPreviewDialog
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.PageNumber = 0
PrintDocument1.Offset = 0
PrintPreviewDialog1.ShowDialog()
End Sub

Dim nudFontSize As Single

'nudFontSize.Value

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal ev As System.Drawing.Printing.PrintPageEventArgs)

Dim fnt As Font
Dim linesPerPage As Integer = 0
Dim yPos As Integer = ev.MarginBounds.Top
Dim count As Integer = 0
Dim leftMargin As Integer = ev.MarginBounds.Left
Dim topMargin As Integer = ev.MarginBounds.Top
Dim line As String = Nothing
Dim fntFamily As FontFamily
Dim fntDoc As FontDocument = DirectCast(sender, FontDocument)
fntDoc.PageNumber += 1

If fntDoc.FontFamilies.Length = 0 Then ev.Cancel = True

While yPos < ev.MarginBounds.Bottom AndAlso fntDoc.Offset <
fntDoc.FontFamilies.Length - 1

fntFamily = fntDoc.FontFamilies(fntDoc.Offset)
Try
fnt = New Font(fntFamily, nudFontSize.Value,
FontStyle.Regular)
Catch ex As Exception
'MsgBox(ex.Message) I see :)
End Try

ev.Graphics.DrawString(fntFamily.Name.Substring(0, 2), fnt,
Brushes.Black, 150, yPos)
If yPos > ev.MarginBounds.Bottom Then
ev.HasMorePages = True
Exit Sub
Else
ev.HasMorePages = False
End If
yPos += fnt.Height
fntDoc.Offset += 1

End While
ev.HasMorePages = False

End Sub






Chuck Gantz ha scritto:
Hi Tom,

Thanks for the reply.

The lines
yPos = ev.MarginBounds.Top
fntDoc.Offset = 0
fntDoc.PageNumber = 0

actually are in the program. (It would be nice if the formatting would have
copied over so that my code could be read a little easier). The offset and
PageNumber variables can't be set in PrintDocument1_PrintPage since they
change for each line of text and page.

Also, I don't use a messageBox in the catch clause because the code doesn't
reach there for an actual error. Some font families don't have a regular
font style. When that happens the line
fnt = New Font(fntFamily, nudFontSize.Value, FontStyle.Regular)

throws an exception. The catch clause keeps the program from ending because
of that exception.

Chuck

Hi Chuck. At first sight, it would seem a good idea to add

yPos = ev.MarginBounds.Top
fntDoc.Offset = 0
fntDoc.PageNumber = 0

[and any other var needing reset]

after

Dim fntDoc As FontDocument = CType(sender, FontDocument)

(where you could actually use a DirectCast instead of ctype. You migh
want to to place a MsgBox(ex.Message, MsgBoxStyle.Information) in
the catch clause)....


-tom

Chuck Gantz ha scritto:
I'm using vb2005.

I wrote a simple app to view and print all the fonts on my pc.To see how
the
printout will look without wasting lots of paper, I use
PrintPreviewDialog.

The first time I run PrintPreviewDialog, it correctly shows five pages of
printout. If I run it a second time, it only shows three pages, with the
original second page printing over the original first page.
If I run it a third time, only one page shows up, with all the fonts
being
printed on that page.

Can anyone see what's wrong. Thanks.
p.s., I'm not sure why when I copy my code all my formatting dissapeared.

'First class
Imports System.Drawing.Printing
Public Class FontDocument
Inherits printdocument
Private m_fontFamilies() As FontFamily
Private m_pageNumber As Int32
Private m_offset As Int32
Public Property FontFamilies() As FontFamily()
Get
Return m_fontFamilies
End Get
Set(ByVal value As FontFamily())
m_fontFamilies = value
End Set
End Property
Public Property PageNumber() As Int32
Get
Return m_pageNumber
End Get
Set(ByVal value As Int32)
m_pageNumber = value
End Set
End Property
Public Property Offset() As Int32
Get
Return m_offset
End Get
Set(ByVal value As Int32)
m_offset = value
End Set
End Property
Public Sub FontDocument(ByVal fontFamilies() As FontFamily)
Me.m_fontFamilies = fontFamilies
End Sub
End Class

'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PrintDocument1 = New FontDocument
PrintDocument1.FontFamilies = FontFamily.Families
nudFontSize.Value = 10
LoadFontsRichTextBox()
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PrintPreviewToolStripMenuItem.Click
AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
Dim PrintPreviewDialog1 As PrintPreviewDialog = New
System.Windows.Forms.PrintPreviewDialog
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.PageNumber = 0
PrintDocument1.Offset = 0
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal
ev
As System.Drawing.Printing.PrintPageEventArgs)
Dim fnt As Font
Dim linesPerPage As Integer = 0
Dim yPos As Integer = ev.MarginBounds.Top
Dim count As Integer = 0
Dim leftMargin As Integer = ev.MarginBounds.Left
Dim topMargin As Integer = ev.MarginBounds.Top
Dim line As String = Nothing
Dim fntFamily As FontFamily
If sender Is Nothing OrElse ev Is Nothing Then
Return
End If
Dim fntDoc As FontDocument = CType(sender, FontDocument)
fntDoc.PageNumber += 1
While yPos < ev.MarginBounds.Bottom And fntDoc.Offset <
fntDoc.FontFamilies.Length
fntFamily = fntDoc.FontFamilies(fntDoc.Offset)
Try
fnt = New Font(fntFamily, nudFontSize.Value, FontStyle.Regular)
ev.Graphics.DrawString(fntFamily.Name, fnt, Brushes.Black, 150, yPos)
yPos += fnt.Height

If yPos > ev.MarginBounds.Bottom Then
ev.HasMorePages = True
Return
Else
ev.HasMorePages = False
End If
Catch ex As Exception
Finally
fntDoc.Offset += 1
End Try
End While
ev.HasMorePages = False
End Sub
 
C

Chuck Gantz

You're right. I moved the line AddHandler PrintDocument1.PrintPage,
AddressOf PrintDocument1_PrintPage from the menu item handler to the form
load handler so that PrintDocument1_PrintPage is only added once. That took
care of the problem.

I always thought it didn't matter how many times a handler was added. I
guess I was wrong.

Thanks

Chuck

Yes it's actually uncomfortable to read, but gets better in the editor
:) I have good new. I think I located your problem. You are attacching
multiple handlers ...

Replace this part:

'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
PrintDocument1 = New FontDocument
AddHandler PrintDocument1.PrintPage, AddressOf
PrintDocument1_PrintPage
PrintDocument1.FontFamilies = FontFamily.Families
nudFontSize.Value = 10
LoadFontsRichTextBox()
End Sub

Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles Button1.Click '
PrintPreviewToolStripMenuItem.Click
Dim PrintPreviewDialog1 As PrintPreviewDialog = New
System.Windows.Forms.PrintPreviewDialog
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.PageNumber = 0
PrintDocument1.Offset = 0
PrintPreviewDialog1.ShowDialog()
End Sub

Dim nudFontSize As Single

'nudFontSize.Value

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal ev As System.Drawing.Printing.PrintPageEventArgs)

Dim fnt As Font
Dim linesPerPage As Integer = 0
Dim yPos As Integer = ev.MarginBounds.Top
Dim count As Integer = 0
Dim leftMargin As Integer = ev.MarginBounds.Left
Dim topMargin As Integer = ev.MarginBounds.Top
Dim line As String = Nothing
Dim fntFamily As FontFamily
Dim fntDoc As FontDocument = DirectCast(sender, FontDocument)
fntDoc.PageNumber += 1

If fntDoc.FontFamilies.Length = 0 Then ev.Cancel = True

While yPos < ev.MarginBounds.Bottom AndAlso fntDoc.Offset <
fntDoc.FontFamilies.Length - 1

fntFamily = fntDoc.FontFamilies(fntDoc.Offset)
Try
fnt = New Font(fntFamily, nudFontSize.Value,
FontStyle.Regular)
Catch ex As Exception
'MsgBox(ex.Message) I see :)
End Try

ev.Graphics.DrawString(fntFamily.Name.Substring(0, 2), fnt,
Brushes.Black, 150, yPos)
If yPos > ev.MarginBounds.Bottom Then
ev.HasMorePages = True
Exit Sub
Else
ev.HasMorePages = False
End If
yPos += fnt.Height
fntDoc.Offset += 1

End While
ev.HasMorePages = False

End Sub






Chuck Gantz ha scritto:
Hi Tom,

Thanks for the reply.

The lines
yPos = ev.MarginBounds.Top
fntDoc.Offset = 0
fntDoc.PageNumber = 0

actually are in the program. (It would be nice if the formatting would
have
copied over so that my code could be read a little easier). The offset
and
PageNumber variables can't be set in PrintDocument1_PrintPage since they
change for each line of text and page.

Also, I don't use a messageBox in the catch clause because the code
doesn't
reach there for an actual error. Some font families don't have a regular
font style. When that happens the line
fnt = New Font(fntFamily, nudFontSize.Value, FontStyle.Regular)

throws an exception. The catch clause keeps the program from ending
because
of that exception.

Chuck

Hi Chuck. At first sight, it would seem a good idea to add

yPos = ev.MarginBounds.Top
fntDoc.Offset = 0
fntDoc.PageNumber = 0

[and any other var needing reset]

after

Dim fntDoc As FontDocument = CType(sender, FontDocument)

(where you could actually use a DirectCast instead of ctype. You migh
want to to place a MsgBox(ex.Message, MsgBoxStyle.Information) in
the catch clause)....


-tom

Chuck Gantz ha scritto:

I'm using vb2005.

I wrote a simple app to view and print all the fonts on my pc.To see
how
the
printout will look without wasting lots of paper, I use
PrintPreviewDialog.

The first time I run PrintPreviewDialog, it correctly shows five pages
of
printout. If I run it a second time, it only shows three pages, with
the
original second page printing over the original first page.
If I run it a third time, only one page shows up, with all the fonts
being
printed on that page.

Can anyone see what's wrong. Thanks.
p.s., I'm not sure why when I copy my code all my formatting
dissapeared.

'First class
Imports System.Drawing.Printing
Public Class FontDocument
Inherits printdocument
Private m_fontFamilies() As FontFamily
Private m_pageNumber As Int32
Private m_offset As Int32
Public Property FontFamilies() As FontFamily()
Get
Return m_fontFamilies
End Get
Set(ByVal value As FontFamily())
m_fontFamilies = value
End Set
End Property
Public Property PageNumber() As Int32
Get
Return m_pageNumber
End Get
Set(ByVal value As Int32)
m_pageNumber = value
End Set
End Property
Public Property Offset() As Int32
Get
Return m_offset
End Get
Set(ByVal value As Int32)
m_offset = value
End Set
End Property
Public Sub FontDocument(ByVal fontFamilies() As FontFamily)
Me.m_fontFamilies = fontFamilies
End Sub
End Class

'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PrintDocument1 = New FontDocument
PrintDocument1.FontFamilies = FontFamily.Families
nudFontSize.Value = 10
LoadFontsRichTextBox()
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PrintPreviewToolStripMenuItem.Click
AddHandler PrintDocument1.PrintPage, AddressOf
PrintDocument1_PrintPage
Dim PrintPreviewDialog1 As PrintPreviewDialog = New
System.Windows.Forms.PrintPreviewDialog
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.PageNumber = 0
PrintDocument1.Offset = 0
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal
ev
As System.Drawing.Printing.PrintPageEventArgs)
Dim fnt As Font
Dim linesPerPage As Integer = 0
Dim yPos As Integer = ev.MarginBounds.Top
Dim count As Integer = 0
Dim leftMargin As Integer = ev.MarginBounds.Left
Dim topMargin As Integer = ev.MarginBounds.Top
Dim line As String = Nothing
Dim fntFamily As FontFamily
If sender Is Nothing OrElse ev Is Nothing Then
Return
End If
Dim fntDoc As FontDocument = CType(sender, FontDocument)
fntDoc.PageNumber += 1
While yPos < ev.MarginBounds.Bottom And fntDoc.Offset <
fntDoc.FontFamilies.Length
fntFamily = fntDoc.FontFamilies(fntDoc.Offset)
Try
fnt = New Font(fntFamily, nudFontSize.Value, FontStyle.Regular)
ev.Graphics.DrawString(fntFamily.Name, fnt, Brushes.Black, 150, yPos)
yPos += fnt.Height

If yPos > ev.MarginBounds.Bottom Then
ev.HasMorePages = True
Return
Else
ev.HasMorePages = False
End If
Catch ex As Exception
Finally
fntDoc.Offset += 1
End Try
End While
ev.HasMorePages = False
End Sub
 
T

tommaso.gastaldi

ah, you surely have already adjusted it, but just in case...

fntFamily.Name.Substring(0, 2) should be in fntFamily.Name and
Handles Button1.Click should be (in your code)
PrintPreviewToolStripMenuItem.Click
Dim nudFontSize As Single, 'nudFontSize.Value should be removed

I just forget to remove these part I added to play with your code.

[actually I would also simplify it a little bit, and I will try to dig
more on the interesting question of the "unsupported" Regular style. If
the try catch could be replaced with a "if then" it would be much
better, as I can see the try catch is slowing down the procedure...let
me know if you find out a way to detect that]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top