473,387 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Correct Disposal of Pen Object

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cameron | last post by:
I am in a situation in which I am recieving a DirectoryEntry that may or may not be already be disposed. I need a very fast, (from a code point of view not necissarily a coding point of view), way...
2
by: JC | last post by:
On the second pass through the obj I get this error. This is all running on my machine. I used the framework wizard to trust the interop.mydll.dll assembly, what am I missing, do I force a disposal...
4
by: Joe Kimbler | last post by:
I've heard this both ways so I'm wondering, is using the: Me.Dispose () ....method in VB.NET the best way to close down an application? Is there a cleaner way to do it? I've heard grumblings...
2
by: Robert Vasquez | last post by:
I have three classes. One (Class ObjectC) and two other classes (Class1 and Class2) that will hold instances of the ObjectC class. I would like to transfer an instance of OjectC from Class1 into...
1
by: Lee | last post by:
Hi there, Not sure if this is the most relevent newsgroup so please redirect me if needed. I just have a general query regarding how memory disposal works in relation to the DOM object model....
4
by: randy1200 | last post by:
I have a SideForm. If the use presses a button on the main form, the side form comes up by executing the following: SideForm sf = new SideForm() sf.ShowDialog(); This works perfectly. I've...
2
by: mukulprabhu | last post by:
Hello there, Is it compulsory that the component objects created in code behind should be disposed explicitly. For eg. first.cs index.aspx.cs...
7
by: Steve | last post by:
I have a method that creates of new form each time its called do i need to dipose of the form when it closes or doe the garbage collector take care of it? code private void button1_Click(object...
1
by: Chris Dunaway | last post by:
When working with GDI+, calling the CreateGraphics method to draw on a control has normally been frowned upon and it is always emphasized to dispose of pens and brushes and other GDI objects lest...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.