CollectionBase Remove method - still trying

M

m. pollack

Hi all,

I'm still trying to get to the bottom of the problem I am
having with the CollectionBase class and the Object
Collection Editor.

Briefly put, I am exposing a strongly-typed collection
property, using a class derived from CollectionBase, to
the user via the PropertyGrid control's popup Collection
Editor. I need to know when the user has added or removed
objects to the collection. In my collection class I am
implementing the collection interface's Add, Insert,
Remove, and RemoveAt methods, but the Collection Editor
does not seem to call these implemented methods to
manipulate the list. There are some "hook" methods
provided, namely OnInsert, OnInsertComplete, OnRemove, and
OnRemoveComplete, so I tried overriding those methods. If
the user adds to the collection with the Collection
Editor, the OnInsert hook is called, but the OnRemove
method is not called when an object is removed. I can find
absolutely no documentation about this problem, and as far
as my application is concerned it's kind of a show-stopper.
A few people replied to my previous post, but I still have
not found a way around the problem. JD posted that the IL
did in fact seem to be calling the On Remove and
OnRemoveComplete methods, but it sure doesn't seem to be
working when I try it. Mick Doherty posted that the
CollectionEditor might be destroying and recreating the
whole list, and thereby bypassing this code... does
anybody know what is going on with this thing?

Many thanks, MP
 
M

Mick Doherty

I had another look at this problem.
You need to override the CollectionEditors DestroyInstance method.

Heres some VB.Net Code, needs a bit more work but it's a start.
You'll need to add a reference to System.Design.dll.

\\\
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Design

Public Class MyClass
...
...


<DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
Editor(GetType(MyCollectionEditor), GetType(UITypeEditor))> _
Public ReadOnly Property MyCollection() As MyCollectionType
Get
Return MyCollectionTypeVariable
End Get
End Property
Private Function ShouldSerializeMyCollection() As Boolean
Return False
End Function
...
...

Friend Class MyCollectionEditor
Inherits CollectionEditor

Public Sub New(ByVal type As System.Type)
MyBase.new(type)
End Sub

Protected Overrides Function CreateCollectionItemType() As
System.Type
Return GetType(MyType)
End Function

Protected Overrides Sub DestroyInstance(ByVal instance As Object)
MyBase.DestroyInstance(instance)
'It's as near as I can get, and only happens if OK is pressed.
If TypeOf instance is MyCollectionTypeItem Then
MsgBox(DirectCast(instance,
MyCollectionItemType).Variable.ToString)
End If
End Sub

End Class

End Class
///
 

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