CollectionBase OnInsert

M

m. pollack

Hi all,

I have subclassed the CollectionEditor object in order to
override its OnInsert and OnRemove methods (because I am
exposing some properties (including a collection property)
of a class object at runtime with a PropertyGrid, and
needed to know when the user was adding objects to, or
removing objects from, the collection. My question is: Can
I use the OnInsert method to block the user from modifying
collection if the object to be added/removed fails some
test? OnInsert and OnRemove just return "void", so I don't
see how, but the docs suggest that it can be done.

One complication of using the CollectionEditor is that it
seems not to add/remove objects by calling the collection
class's Add or Remove methods (removal, for instance,
seems to just happen somehow via
(CollectionEditor.DestroyInstance()), but rather by
grabbing the whole inner list, working with it "offline",
and then just plunking it back in.

Any ideas?
 
M

Mick Doherty

You can stop the user adding an object to the collection by throwing an
Exception/ArgumentException in the OnValidate methods of the CollectionBase
class. This will force the user to either correct the Object or remove it.

You can use the CollectionEditor's CanRemoveInstance() Method to stop the
user removing an object.
 
M

m. pollack

Hi Mick,

Thanks again for responding.
OnValidate? I'll give that a go. I am interested in being
able to substitute a different object for the one being
added - in other words I want to replace the object
created by the Editor with a same-type object of my own
creation, with various settings of its data members. I'm
hoping I can do something like simply replace the object
reference of the passed-in argument with the new one.
Anyway, I'll try.. thanks!

PS ...you'd think they might make this gizmo a little more
developer-friendly! Wherever there is user input,
developers are going to want to control the process --
this CollectionEditor does all of this in avery non-
intuitive way (why, for example, can't OnInsert, instead
of returning 'void', return a bool that cancels the
operation if false?). If it would simply call the
appropriate methods in the collection class (Remove, Add,
etc.) instead of all this behind-the-scenes monkeying
about, life would be much simpler...

MP
 

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