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 PrintPreviewDia log.
The first time I run PrintPreviewDia log, 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(By Val fontFamilies() As FontFamily)
Me.m_fontFamili es = fontFamilies
End Sub
End Class
'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Loa d(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
PrintDocument1 = New FontDocument
PrintDocument1. FontFamilies = FontFamily.Fami lies
nudFontSize.Val ue = 10
LoadFontsRichTe xtBox()
End Sub
Private Sub PrintPreviewToo lStripMenuItem_ Click(ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
PrintPreviewToo lStripMenuItem. Click
AddHandler PrintDocument1. PrintPage, AddressOf PrintDocument1_ PrintPage
Dim PrintPreviewDia log1 As PrintPreviewDia log = New
System.Windows. Forms.PrintPrev iewDialog
PrintPreviewDia log1.Document = PrintDocument1
PrintDocument1. PageNumber = 0
PrintDocument1. Offset = 0
PrintPreviewDia log1.ShowDialog ()
End Sub
Private Sub PrintDocument1_ PrintPage(ByVal sender As System.Object, ByVal ev
As System.Drawing. Printing.PrintP ageEventArgs)
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.PageNumb er += 1
While yPos < ev.MarginBounds .Bottom And fntDoc.Offset <
fntDoc.FontFami lies.Length
fntFamily = fntDoc.FontFami lies(fntDoc.Off set)
Try
fnt = New Font(fntFamily, nudFontSize.Val ue, FontStyle.Regul ar)
ev.Graphics.Dra wString(fntFami ly.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
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 PrintPreviewDia log.
The first time I run PrintPreviewDia log, 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(By Val fontFamilies() As FontFamily)
Me.m_fontFamili es = fontFamilies
End Sub
End Class
'Second class
Dim PrintDocument1 As FontDocument
Private Sub FontPrinter_Loa d(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
PrintDocument1 = New FontDocument
PrintDocument1. FontFamilies = FontFamily.Fami lies
nudFontSize.Val ue = 10
LoadFontsRichTe xtBox()
End Sub
Private Sub PrintPreviewToo lStripMenuItem_ Click(ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
PrintPreviewToo lStripMenuItem. Click
AddHandler PrintDocument1. PrintPage, AddressOf PrintDocument1_ PrintPage
Dim PrintPreviewDia log1 As PrintPreviewDia log = New
System.Windows. Forms.PrintPrev iewDialog
PrintPreviewDia log1.Document = PrintDocument1
PrintDocument1. PageNumber = 0
PrintDocument1. Offset = 0
PrintPreviewDia log1.ShowDialog ()
End Sub
Private Sub PrintDocument1_ PrintPage(ByVal sender As System.Object, ByVal ev
As System.Drawing. Printing.PrintP ageEventArgs)
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.PageNumb er += 1
While yPos < ev.MarginBounds .Bottom And fntDoc.Offset <
fntDoc.FontFami lies.Length
fntFamily = fntDoc.FontFami lies(fntDoc.Off set)
Try
fnt = New Font(fntFamily, nudFontSize.Val ue, FontStyle.Regul ar)
ev.Graphics.Dra wString(fntFami ly.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
Comment