Correct Disposal of Pen Object

C

Chris

I am just wondering if I did this the best way possible. I needed to
add a double boarder around a label box. So I made a pen in the class,
and do the drawing of the rectangles in the onpaint method. Did I do
the disposal right? Is there a better way to do this? Thanks

Chris

Public Class ErrorMessageBox
Inherits Label

Dim m_Pen As Drawing.Pen

Sub New()
m_Pen = New Drawing.Pen(Color.Black)
End Sub

Private Overloads Sub dispose()
m_Pen.Dispose()
MyBase.dispose()
End Sub

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim StringSize As Drawing.SizeF =
e.Graphics.MeasureString(Me.Text, Me.Font)
Me.Width = CInt(StringSize.Width) + 40
Me.Height = CInt(StringSize.Height) + 40
Me.BringToFront()

MyBase.OnPaint(e)
e.Graphics.DrawRectangle(m_Pen, 3, 3, Me.Width - 6, Me.Height - 6)
e.Graphics.DrawRectangle(m_Pen, 5, 5, Me.Width - 10, Me.Height
- 10)

End Sub
End Class
 
J

Jay B. Harlow [MVP - Outlook]

Chris,
| Did I do
| the disposal right?
No, you need to override your base class's Dispose method:

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
m_Pen.Dispose()
MyBase.dispose()
End Sub


| Is there a better way to do this? Thanks

Consider using Pens.Black instead of creating a black pen for each
MessageBox.

Consider using SystemPens.WindowFrame instead of a black frame.

Consider adding a BorderColor or BorderPen property to the class to allow
the designer of the form to set the color used... You could default the
BorderColor or BorderPen to Black or WindowFrame...

Hope this helps
Jay


|I am just wondering if I did this the best way possible. I needed to
| add a double boarder around a label box. So I made a pen in the class,
| and do the drawing of the rectangles in the onpaint method. Did I do
| the disposal right? Is there a better way to do this? Thanks
|
| Chris
|
| Public Class ErrorMessageBox
| Inherits Label
|
| Dim m_Pen As Drawing.Pen
|
| Sub New()
| m_Pen = New Drawing.Pen(Color.Black)
| End Sub
|
| Private Overloads Sub dispose()
| m_Pen.Dispose()
| MyBase.dispose()
| End Sub
|
| Protected Overrides Sub OnPaint(ByVal e As
| System.Windows.Forms.PaintEventArgs)
|
| Dim StringSize As Drawing.SizeF =
| e.Graphics.MeasureString(Me.Text, Me.Font)
| Me.Width = CInt(StringSize.Width) + 40
| Me.Height = CInt(StringSize.Height) + 40
| Me.BringToFront()
|
| MyBase.OnPaint(e)
| e.Graphics.DrawRectangle(m_Pen, 3, 3, Me.Width - 6, Me.Height - 6)
| e.Graphics.DrawRectangle(m_Pen, 5, 5, Me.Width - 10, Me.Height
| - 10)
|
| End Sub
| End Class
 
M

Mythran

Jay B. Harlow said:
Chris,
| Did I do
| the disposal right?
No, you need to override your base class's Dispose method:

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
m_Pen.Dispose()
MyBase.dispose()
End Sub

Would you need to check to see if disposing = True before actually
disposing?

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing
m_Pen.Dispose()
End If
MyBase.Dispose()
End Sub

???

Mythran
 
J

Jay B. Harlow [MVP - Outlook]

Mythran,
| Would you need to check to see if disposing = True before actually
| disposing?
That's an excellent question...

Yes seeing as Pen is a managed object, you should only call its Dispose if
disposing is true.

http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp

Thanks for the additional
Jay


|
| | > Chris,
| > | Did I do
| > | the disposal right?
| > No, you need to override your base class's Dispose method:
| >
| > Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
| > m_Pen.Dispose()
| > MyBase.dispose()
| > End Sub
| >
| >
|
| Would you need to check to see if disposing = True before actually
| disposing?
|
| Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
| If disposing
| m_Pen.Dispose()
| End If
| MyBase.Dispose()
| End Sub
|
| ???
|
| Mythran
|
 
M

Mythran

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top