[RESOLVED] Pen and Pens object dilemma
Hi everyone, I hope some one can give me some light on this. I have my CustomPaintEventArgs class that has “Pen” property so that it can be assigned with Pen or Pens object. Now here is the dilemma, I should dispose the pen object as soon as I finish with it but when the CustomPaintEventArgs’ “Pen” property is assigned with a Pens object it throws an exception (Changes cannot be made to Brush because permissions are not valid). How can I determine if the Pen property has Pens object. Actualy when it is used as Pens.Black it returns the Pens Black property which is a pen object.
Is there any solution to this? Thanks in advance.
VBDT :)
Re: Pen and Pens object dilemma
Isnt the pen disposed of automatically when the sub comes to an end? I mean the CustomPaintEventArgs object is disposed isnt it?
Re: Pen and Pens object dilemma
No, Atheist it is not if it is created using the "New" keyword. But I think I found a solution to it. Here is an example:
vb Code:
'This one must be disposed
Dim p As New Pen(Color.Red, 2)
e.Pen = p
p.Dispose()
'This one should not and can't be disposed.
e.Pen = Pens.Red
With the solution I cam up is this. In the paint event handler the developer should dispose the object if it is created using with the “New” keyword after he assigns the pen object to the “Pen” property otherwise he shouldn’t. And in my CustomPaintEventArgs I dispose the current pan and copy the user’s pen to the _pen member which its turn is disposed after it is used.
vb Code:
''' <summary>
''' Gets or sets the pen object that is used to draw the element.
''' </summary>
Public Overloads Property Pen() As Pen
Get
Return Me._pen
End Get
Set(ByVal value As Pen)
Me._pen.Dispose()
Me._pen = value.Clone()
End Set
End Property