PrintPreview problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chuck Gantz

    #1

    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 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


  • tommaso.gastaldi@uniroma1.it

    #2
    Re: PrintPreview problem

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

    yPos = ev.MarginBounds .Top
    fntDoc.Offset = 0
    fntDoc.PageNumb er = 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.Messa ge, MsgBoxStyle.Inf ormation) in
    the catch clause)....


    -tom

    Chuck Gantz ha scritto:
    [color=blue]
    > 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[/color]

    Comment

    • Chuck Gantz

      #3
      Re: PrintPreview problem

      Hi Tom,

      Thanks for the reply.

      The lines
      yPos = ev.MarginBounds .Top
      fntDoc.Offset = 0
      fntDoc.PageNumb er = 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.Val ue, FontStyle.Regul ar)

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

      Chuck

      <tommaso.gastal di@uniroma1.it> wrote in message
      news:1142981155 .720227.95520@j 33g2000cwa.goog legroups.com...[color=blue]
      > Hi Chuck. At first sight, it would seem a good idea to add
      >
      > yPos = ev.MarginBounds .Top
      > fntDoc.Offset = 0
      > fntDoc.PageNumb er = 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.Messa ge, MsgBoxStyle.Inf ormation) in
      > the catch clause)....
      >
      >
      > -tom
      >
      > Chuck Gantz ha scritto:
      >[color=green]
      >> 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[/color]
      >[/color]


      Comment

      • tommaso.gastaldi@uniroma1.it

        #4
        Re: PrintPreview problem

        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_Loa d(ByVal sender As System.Object, ByVal e
        As System.EventArg s) Handles MyBase.Load
        PrintDocument1 = New FontDocument
        AddHandler PrintDocument1. PrintPage, AddressOf
        PrintDocument1_ PrintPage
        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 Button1.Click '
        PrintPreviewToo lStripMenuItem. Click
        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

        Dim nudFontSize As Single

        'nudFontSize.Va lue

        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
        Dim fntDoc As FontDocument = DirectCast(send er, FontDocument)
        fntDoc.PageNumb er += 1

        If fntDoc.FontFami lies.Length = 0 Then ev.Cancel = True

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

        fntFamily = fntDoc.FontFami lies(fntDoc.Off set)
        Try
        fnt = New Font(fntFamily, nudFontSize.Val ue,
        FontStyle.Regul ar)
        Catch ex As Exception
        'MsgBox(ex.Mess age) I see :)
        End Try

        ev.Graphics.Dra wString(fntFami ly.Name.Substri ng(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:
        [color=blue]
        > Hi Tom,
        >
        > Thanks for the reply.
        >
        > The lines
        > yPos = ev.MarginBounds .Top
        > fntDoc.Offset = 0
        > fntDoc.PageNumb er = 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.Val ue, FontStyle.Regul ar)
        >
        > throws an exception. The catch clause keeps the program from ending because
        > of that exception.
        >
        > Chuck
        >
        > <tommaso.gastal di@uniroma1.it> wrote in message
        > news:1142981155 .720227.95520@j 33g2000cwa.goog legroups.com...[color=green]
        > > Hi Chuck. At first sight, it would seem a good idea to add
        > >
        > > yPos = ev.MarginBounds .Top
        > > fntDoc.Offset = 0
        > > fntDoc.PageNumb er = 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.Messa ge, MsgBoxStyle.Inf ormation) in
        > > the catch clause)....
        > >
        > >
        > > -tom
        > >
        > > Chuck Gantz ha scritto:
        > >[color=darkred]
        > >> 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[/color]
        > >[/color][/color]

        Comment

        • Chuck Gantz

          #5
          Re: PrintPreview problem

          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

          <tommaso.gastal di@uniroma1.it> wrote in message
          news:1142990019 .489012.243250@ v46g2000cwv.goo glegroups.com.. .[color=blue]
          > 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_Loa d(ByVal sender As System.Object, ByVal e
          > As System.EventArg s) Handles MyBase.Load
          > PrintDocument1 = New FontDocument
          > AddHandler PrintDocument1. PrintPage, AddressOf
          > PrintDocument1_ PrintPage
          > 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 Button1.Click '
          > PrintPreviewToo lStripMenuItem. Click
          > 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
          >
          > Dim nudFontSize As Single
          >
          > 'nudFontSize.Va lue
          >
          > 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
          > Dim fntDoc As FontDocument = DirectCast(send er, FontDocument)
          > fntDoc.PageNumb er += 1
          >
          > If fntDoc.FontFami lies.Length = 0 Then ev.Cancel = True
          >
          > While yPos < ev.MarginBounds .Bottom AndAlso fntDoc.Offset <
          > fntDoc.FontFami lies.Length - 1
          >
          > fntFamily = fntDoc.FontFami lies(fntDoc.Off set)
          > Try
          > fnt = New Font(fntFamily, nudFontSize.Val ue,
          > FontStyle.Regul ar)
          > Catch ex As Exception
          > 'MsgBox(ex.Mess age) I see :)
          > End Try
          >
          > ev.Graphics.Dra wString(fntFami ly.Name.Substri ng(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:
          >[color=green]
          >> Hi Tom,
          >>
          >> Thanks for the reply.
          >>
          >> The lines
          >> yPos = ev.MarginBounds .Top
          >> fntDoc.Offset = 0
          >> fntDoc.PageNumb er = 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.Val ue, FontStyle.Regul ar)
          >>
          >> throws an exception. The catch clause keeps the program from ending
          >> because
          >> of that exception.
          >>
          >> Chuck
          >>
          >> <tommaso.gastal di@uniroma1.it> wrote in message
          >> news:1142981155 .720227.95520@j 33g2000cwa.goog legroups.com...[color=darkred]
          >> > Hi Chuck. At first sight, it would seem a good idea to add
          >> >
          >> > yPos = ev.MarginBounds .Top
          >> > fntDoc.Offset = 0
          >> > fntDoc.PageNumb er = 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.Messa ge, MsgBoxStyle.Inf ormation) 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
          >> >> 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
          >> >[/color][/color]
          >[/color]


          Comment

          • tommaso.gastaldi@uniroma1.it

            #6
            Re: PrintPreview problem

            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)
            PrintPreviewToo lStripMenuItem. Click
            Dim nudFontSize As Single, 'nudFontSize.Va lue 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 "unsupporte d" 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]

            Comment

            Working...