473,388 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

PrintPreview problem

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
Mar 21 '06 #1
5 1372
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


Mar 21 '06 #2
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

<to**************@uniroma1.it> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
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

Mar 21 '06 #3
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

<to**************@uniroma1.it> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
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


Mar 22 '06 #4
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

<to**************@uniroma1.it> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
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

<to**************@uniroma1.it> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
> 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
>

Mar 22 '06 #5
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]

Mar 22 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LARB | last post by:
Good morning, I have a print routine where I use a "printpreview dialog' control. The text is perfect BUT when I then print the job all the text is completely crazy, a bit like...
1
by: Randy | last post by:
Hello, I'm trying to print a simple string using the PrintPreviewDialog. I get the string to show up in the PrintPreview dialog, but when I click the Printer icon, it just prints a blank page....
11
by: Adam Right | last post by:
Hi, I am using .Net Framework 2.0 final, and i use .Net printing library to print a document. I use printPreview dialog to print the document but after preview was shown, when i press the...
9
by: Adam Right | last post by:
Hi, I am using .Net Framework 2.0 final, and i use .Net printing library to print a document. I use printPreview dialog to print the document but after preview was shown, when i press the...
5
by: Arno R | last post by:
Hi all, I am using the DetectIdleTime code from MS to shut down an app when not in use for a certain time. http://support.microsoft.com/?kbid=210297 I use a hidden form FrmDetectIdeTime with a...
0
by: james | last post by:
While trying to help another programmer on MSDN's Visual Basic 2005 Express Edition forum, he (and I ) ran into a strange problem with the PrintPreview Control in VB2005. If you load a document (...
0
by: Joxie | last post by:
Hi, All! Recently I'm using ultrawebgrid for my projects... While I try Ultragrid (for windows application) I could call printpreview as ultragrid function, but while I'm using Ultrawebgrid (for...
6
by: Stuart Nathan | last post by:
Can someone tell me how to remove a previously loaded PrintDocument in a PrintPreview control, so that I can display a nedw one?
2
by: eBob.com | last post by:
I have a print application modeled on a program which I found via this newsgroup. (I still have comments in German in it!) The model uses ExtendedPrintPreviewDialog but I don't seem to have that...
0
by: vench | last post by:
Hi! I have a vb.net project that uses mozilla activeX control, there is no problem in navigating or loading the url in the control, the problem that I have encountered is when printpreview using the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.