Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fire event before item is added to collection in C#?

I would like to do some processing before an item is added to a BindingList. I see there is an ListChanged event but this is fired after the item is added. The AddingNew event is only fired when the AddNew method (not the Add method) is called. Has anyone done something like this before?

UPDATE:

I have created the following classes and when the Add method is called on the IList, my new Add method gets triggered. So, do I have the casting problem that I've read in other places? If I removed the ISpecialCollection interface from the collection, my Add method doesn't get called. Can someone explain why it's acting differently? Do I have the casting problem if I use the ISpecialCollection< interface?

public interface ISpecialCollection<T> : IList<T>
{
}

public class SpecialCollection<T> : BindingList<T>, ISpecialCollection<T>
{
  public new void Add (T item)  
  {
    base.Add(item);    
  }
}

class Program
{
  static void Main(string[] args)
  {
    IList<ItemType> list = new SpecialCollection<ItemType>();
    list.Add(new ItemType());
  }
}
like image 609
bsh152s Avatar asked Nov 04 '09 14:11

bsh152s


3 Answers

You should override the protected BindingList.InsertItem method (MSDN). Add, Insert and such all call this to do the actual adding adding and raise appropriate events. Raise your event, and then call base.InsertItem to do the rest.

like image 144
dbkk Avatar answered Oct 27 '22 00:10

dbkk


The most straightforward route is to subclass the Collection<T> class. This is the collection class in the BCL which is designed to be subclassed and have it's behavior overriden. Subclassing other types like BindingList<T> or List<T> will just cause you pain.

Once you subclass Collection<T>, you can override Add and create your own event to listen to.

like image 42
JaredPar Avatar answered Oct 27 '22 01:10

JaredPar


I have done something similar as I needed to capture the ItemAdding and ItemAdded event

The magic bit is the new keyword that will override the inherited class' method

// class that inherits generic List and hides the add item
public class ListWithEvents<T> : List<T>
    {
        public event EventHandler ItemAdding;
        public event EventHandler ItemAdded;

        public new void Add(T item)
        {
            if (ItemAdding != null)
                ItemAdding(item, EventArgs.Empty);

            base.Add(item);

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

        }
    }

// Using the class
protected override void OnLoad(EventArgs e)
    {

        ListWithEvents<int> lstI = new ListWithEvents<int>();
        lstI.ItemAdded += new EventHandler(lstI_ItemAdded);
        lstI.ItemAdding += new EventHandler(lstI_ItemAdding);
    }

    void lstI_ItemAdding(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

    void lstI_ItemAdded(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
like image 27
Tim Jarvis Avatar answered Oct 26 '22 23:10

Tim Jarvis