none
Custom collection with custom events RRS feed

  • Question

  • Hello everyone!

    I have some problems implementing a custom collection for .NET 2.0. So, what I need is a strong typed collection able to fire some events each time an item is added or removed. 
    I also need full support for the Collection Editor (available for collection members in the Property Grid control).

    I did some research about this and I managed to create a custom collection with add and remove events inheriting the CollectionBase class and implementing the IList<T> interface. 
    It work fine by code but my events are not fired by the Collection Editor.

    My current aproach is to inherit from Collection<T> class and override the InsertItem()  method. With this I can fire my add event by presing the Add button from the Collection Editor. The problem is I didn't manage to find a method to override for rising the remove event by pressing the Remove button.

    Another problem is the InsertItem() method is called several times, depending how many items are in my collection at that moment, for each item I add into it.

    Here is my code:

    public class UnitList : Collection<Unit>
    {
    public delegate void ItemAddedEventHandler(object sender, EventArgs e);
    public event ItemAddedEventHandler ItemAddedEvent;

    protected override void InsertItem(int index, Unit item)
    {
    base.InsertItem(index, item);

    if (this.ItemAddedEvent != null)
    {
    this.ItemAddedEvent(this, new EventArgs());
    }
    }

    }

    Friday, March 21, 2008 6:00 PM

Answers

  • eugen.ciuta,

     

    According to your question on Collection Editor issues including fire some events and the item added or removed, I would like to provide you the examples as follows:

     

    1. Make a TypeConverter for a collection in Visual Basic .NET

     

    Code Snippet

      Protected Overrides Sub OnInsert(ByVal index As _

            Integer, ByVal value As Object)

            If Not value.GetType() Is GetType(StreetAddress) _

                Then

                Throw New ArgumentException("Value must be of " & _

                    "type StreetAddress.", "value")

            End If

        End Sub

     

        Protected Overrides Sub OnRemove(ByVal index As _

            Integer, ByVal value As Object)

            If Not value.GetType() Is GetType(StreetAddress) _

                Then

                Throw New ArgumentException("Value must be of " & _

                    "type StreetAddress.", "value")

            End If

        End Sub

     

    2. Customized display of collection data in a PropertyGrid

     

    The PropertyGrid is widely used to display an object's properties and values. In previous articles I have shown how to customize and localize the displayed property names and property descriptions (Globalized Property Grid and Globalized Property Grid - Revisited).

    This time I focus myself on Collections and its data. I want to show how you can customize the way a collection displays the objects it contains in a PropertyGrid.

    If you assign an array to the PropertyGrid, you'll see that the PropertyGrid displays all the objects contained within the array

     

    In addition, there are useful references on Collection Editor support:

     

    4. Editing Multiple Types of Objects with Collection Editor and Serializing Objects

     

    5. Developing a custom collection editor

     

    6. How to Edit and Persist Collections with CollectionEditor

     

    Hope that can provide you some idea.

    Monday, March 24, 2008 7:32 AM

All replies

  • Will this work for you?

    Code Snippet

        public class UnitList : Collection<Unit>
        {
            public event EventHandler ItemAdded;
            public event EventHandler ItemRemoved;

            protected override void InsertItem(int index, Unit item)
            {
                base.InsertItem(index, item);

                if (ItemAdded != null)
                    ItemAdded(this, EventArgs.Empty);
            }

            protected override void RemoveItem(int index)
            {
                base.RemoveItem(index);

                if (ItemRemoved != null)
                    ItemRemoved(this, EventArgs.Empty);
            }
        }


    Friday, March 21, 2008 7:45 PM
  • Thanks for your quick reply Philippe but it doesn't work! Sad

    I did this before and it seems that the RemoveItem(int index) method is not called when I press the Remove button from the Collection Editor. 
    So the Collection Editor calls the InsertItem() method when the Add button is pressed but 
    it doesn't call RemoveItem() method when the Remove button is pressed.

    I really don't understand how the Collection Editor works!
    Friday, March 21, 2008 7:55 PM
  • I'm sorry, but I don't have any experience with the collection editor. I would expect that overriding the RemoveItem() method would result in this method being called...

    Someone else?
    Friday, March 21, 2008 8:04 PM
  • eugen.ciuta,

     

    According to your question on Collection Editor issues including fire some events and the item added or removed, I would like to provide you the examples as follows:

     

    1. Make a TypeConverter for a collection in Visual Basic .NET

     

    Code Snippet

      Protected Overrides Sub OnInsert(ByVal index As _

            Integer, ByVal value As Object)

            If Not value.GetType() Is GetType(StreetAddress) _

                Then

                Throw New ArgumentException("Value must be of " & _

                    "type StreetAddress.", "value")

            End If

        End Sub

     

        Protected Overrides Sub OnRemove(ByVal index As _

            Integer, ByVal value As Object)

            If Not value.GetType() Is GetType(StreetAddress) _

                Then

                Throw New ArgumentException("Value must be of " & _

                    "type StreetAddress.", "value")

            End If

        End Sub

     

    2. Customized display of collection data in a PropertyGrid

     

    The PropertyGrid is widely used to display an object's properties and values. In previous articles I have shown how to customize and localize the displayed property names and property descriptions (Globalized Property Grid and Globalized Property Grid - Revisited).

    This time I focus myself on Collections and its data. I want to show how you can customize the way a collection displays the objects it contains in a PropertyGrid.

    If you assign an array to the PropertyGrid, you'll see that the PropertyGrid displays all the objects contained within the array

     

    In addition, there are useful references on Collection Editor support:

     

    4. Editing Multiple Types of Objects with Collection Editor and Serializing Objects

     

    5. Developing a custom collection editor

     

    6. How to Edit and Persist Collections with CollectionEditor

     

    Hope that can provide you some idea.

    Monday, March 24, 2008 7:32 AM