CollectionBase and Updating an Item
-
If you create a custom collection by deriving from CollectionBase, is there an easy way to fire an event when one of the collection items is updated? For example, any GUIs that care can subscribe to some sort of event that would be fired where an object that is a member of the collection is modified?
-
If you create a custom collection by deriving from CollectionBase, is there an easy way to fire an event when one of the collection items is updated? For example, any GUIs that care can subscribe to some sort of event that would be fired where an object that is a member of the collection is modified?
If you have created a custom class you can create whatever events make sense. For instance, in your override for the OnSet method fire off your custom event.
I know the language. I've read a book. - _Madmatt
-
If you create a custom collection by deriving from CollectionBase, is there an easy way to fire an event when one of the collection items is updated? For example, any GUIs that care can subscribe to some sort of event that would be fired where an object that is a member of the collection is modified?
Not that I know of. There are two easy options. 1. If you are using .NET framework 3.5 then add a reference to WindowsBase to your project. You can then use
System.Collections.ObjectModel.ObservableCollection<T>
which has an eventCollectionChanged
which should do what you want. 2. Make your own generic class and implementICollection<T>
and possiblyICollection
. You can simply wrap aList<T>
in this class to make implementation easier. Now you have control over all items in/out of the collection so can raise custom events as you like. [Edit] I've just had a look at the CollectionBase class and you should be able to override the necessary methods as they are protected virtual and raise your own event(s). OnClear, OnInsert, OnRemove, OnSet All these also have a protected virtual OnxxxComplete method too. [/Edit]Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)modified on Sunday, March 21, 2010 10:09 AM