|
-
Jul 16th, 2006, 07:21 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] [02/03] Print document fonts
I have the following sub that handles printing of a document:
VB Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim boldfont As New Font("Arial", 16, FontStyle.Bold)
Dim smallfont As New Font("Arial", 10, FontStyle.Regular)
Dim regularfont As New Font("Arial", 16, FontStyle.Regular)
Dim ulboldfont As New Font("Arial", 16, FontStyle.Bold Or FontStyle.Underline)
Dim footerfont As New Font("Arial", 8, FontStyle.Regular)
With e.Graphics
' Printing code
End With
e.HasMorePages = False
boldfont.Dispose()
smallfont.Dispose()
regularfont.Dispose()
ulboldfont.Dispose()
footerfont.Dispose()
e.Graphics.Dispose()
End Sub
Is it good practice to dispose of the fonts and the graphics object at the end, or is this not necessary?
Thanks.
-
Jul 16th, 2006, 07:55 AM
#2
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:
Private boldfont As Font
Private smallfont As Font
Private regularfont As Font
Private ulboldfont As Font
Private footerfont As Font
Private fontsCreated As Boolean = False
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
If Not Me.fontsCreated Then
boldfont = New Font("Arial", 16, FontStyle.Bold)
smallfont = New Font("Arial", 10, FontStyle.Regular)
regularfont = New Font("Arial", 16, FontStyle.Regular)
ulboldfont = New Font("Arial", 16, FontStyle.Bold Or FontStyle.Underline)
footerfont = New Font("Arial", 8, FontStyle.Regular)
fontsCreated = True
End If
'...
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If fontsCreated Then
boldfont.Dispose()
smallfont.Dispose()
regularfont.Dispose()
ulboldfont.Dispose()
footerfont.Dispose()
End If
End Sub
Last edited by jmcilhinney; Jul 16th, 2006 at 09:21 AM.
-
Jul 16th, 2006, 08:11 AM
#3
Thread Starter
Fanatic Member
Re: [02/03] Print document fonts
Thankyou for the quick reply, much appreciated.
-
Jul 16th, 2006, 09:20 AM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|