Results 1 to 4 of 4

Thread: [RESOLVED] [02/03] Print document fonts

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Resolved [RESOLVED] [02/03] Print document fonts

    I have the following sub that handles printing of a document:
    VB Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.        
    3.         Dim boldfont As New Font("Arial", 16, FontStyle.Bold)
    4.         Dim smallfont As New Font("Arial", 10, FontStyle.Regular)
    5.         Dim regularfont As New Font("Arial", 16, FontStyle.Regular)
    6.         Dim ulboldfont As New Font("Arial", 16, FontStyle.Bold Or FontStyle.Underline)
    7.         Dim footerfont As New Font("Arial", 8, FontStyle.Regular)
    8.              
    9.         With e.Graphics
    10.  
    11.         ' Printing code
    12.                    
    13.         End With
    14.  
    15.         e.HasMorePages = False
    16.  
    17.         boldfont.Dispose()
    18.         smallfont.Dispose()
    19.         regularfont.Dispose()
    20.         ulboldfont.Dispose()
    21.         footerfont.Dispose()
    22.  
    23.         e.Graphics.Dispose()
    24.  
    25.     End Sub

    Is it good practice to dispose of the fonts and the graphics object at the end, or is this not necessary?

    Thanks.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Print document fonts

    You absolutely should NOT be Disposing the Graphics object. You didn't create it so you should not be destroying it. It will be destoyed by the PrintDocument object that created it. I'm aware of at least one person who was getting an exception thrown after their PrintPage event handler because they were Disposing the Graphics object. The Font objects you should be disposing, but I'd not be inclined to create and destroy five font objects every time I printed. I'd be more inclined to do something like this:
    VB Code:
    1. Private boldfont As Font
    2. Private smallfont As Font
    3. Private regularfont As Font
    4. Private ulboldfont As Font
    5. Private footerfont As Font
    6.  
    7. Private fontsCreated As Boolean = False
    8.  
    9. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
    10.                                      ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    11.     If Not Me.fontsCreated Then
    12.         boldfont = New Font("Arial", 16, FontStyle.Bold)
    13.         smallfont = New Font("Arial", 10, FontStyle.Regular)
    14.         regularfont = New Font("Arial", 16, FontStyle.Regular)
    15.         ulboldfont = New Font("Arial", 16, FontStyle.Bold Or FontStyle.Underline)
    16.         footerfont = New Font("Arial", 8, FontStyle.Regular)
    17.  
    18.         fontsCreated = True
    19.     End If
    20.  
    21.     '...
    22. End Sub
    23.  
    24. Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    25.     If fontsCreated Then
    26.         boldfont.Dispose()
    27.         smallfont.Dispose()
    28.         regularfont.Dispose()
    29.         ulboldfont.Dispose()
    30.         footerfont.Dispose()
    31.     End If
    32. End Sub
    Last edited by jmcilhinney; Jul 16th, 2006 at 09:21 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [02/03] Print document fonts

    Thankyou for the quick reply, much appreciated.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [02/03] Print document fonts

    I made a mistake with the code I provided. I've editied it since. The idea was to declare the Font objects at the class level but only create them if and when they are needed, then destroy them once when the form is closed if required. There's a trade-off involved because I've said myself that it is preferable to keep the scope of any variable as narrow as possible. By that "rule" you would want the Fonts declared within the PrintPage event handler, but by doing it this way you keep the number of objects created and destroyed to a minimum. If you know for a fact that the PrintPage event handler will only be executed once then that would not be necessary.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width