By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

Correct Disposal of Pen Object

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"Chris" <no@spam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
|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
Nov 21 '05 #2

P: n/a

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O2**************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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/de...izeDispose.asp

Thanks for the additional
Jay
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
| news:O2**************@TK2MSFTNGP15.phx.gbl...
| > 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
|
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.