Results 1 to 3 of 3

Thread: [RESOLVED] Pen and Pens object dilemma

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Resolved [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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. 'This one must be disposed
    2. Dim p As New Pen(Color.Red, 2)
    3. e.Pen = p
    4. p.Dispose()
    5.  
    6. 'This one should not and can't be disposed.
    7. 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:
    1. ''' <summary>
    2.     ''' Gets or sets the pen object that is used to draw the element.
    3.     ''' </summary>
    4.     Public Overloads Property Pen() As Pen
    5.         Get
    6.             Return Me._pen
    7.         End Get
    8.         Set(ByVal value As Pen)
    9.             Me._pen.Dispose()
    10.             Me._pen = value.Clone()
    11.         End Set
    12.     End Property

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width