Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle add to list event?

I have a list like this:

List<Controls> list = new List<Controls>

How to handle adding new position to this list?

When I do:

myObject.myList.Add(new Control());

I would like to do something like this in my object:

myList.AddingEvent += HandleAddingEvent

And then in my HandleAddingEvent delegate handling adding position to this list. How should I handle adding new position event? How can I make this event available?

like image 455
Tom Smykowski Avatar asked Aug 19 '09 13:08

Tom Smykowski


4 Answers

I believe What you're looking for is already part of the API in the ObservableCollection(T) class. Example:

ObservableCollection<int> myList = new ObservableCollection<int>();  myList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(     delegate(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)                         {         if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)         {             MessageBox.Show("Added value");         }     } );  myList.Add(1); 
like image 135
MattH Avatar answered Sep 17 '22 18:09

MattH


You could inherit from List and add your own handler, something like

using System; using System.Collections.Generic;  namespace test {     class Program     {          class MyList<T> : List<T>         {              public event EventHandler OnAdd;              public new void Add(T item) // "new" to avoid compiler-warnings, because we're hiding a method from base-class             {                 if (null != OnAdd)                 {                     OnAdd(this, null);                 }                 base.Add(item);             }         }          static void Main(string[] args)         {             MyList<int> l = new MyList<int>();             l.OnAdd += new EventHandler(l_OnAdd);             l.Add(1);         }          static void l_OnAdd(object sender, EventArgs e)         {             Console.WriteLine("Element added...");         }     } } 

Warning

  1. Be aware that you have to re-implement all methods which add objects to your list. AddRange() will not fire this event, in this implementation.

  2. We did not overload the method. We hid the original one. If you Add() an object while this class is boxed in List<T>, the event will not be fired!

MyList<int> l = new MyList<int>(); l.OnAdd += new EventHandler(l_OnAdd); l.Add(1); // Will work  List<int> baseList = l; baseList.Add(2); // Will NOT work!!! 
like image 29
Paolo Tedesco Avatar answered Sep 20 '22 18:09

Paolo Tedesco


What you need is a class that has events for any type of modification that occurs in the collection. The best class for this is BindingList<T>. It has events for every type of mutation which you can then use to modify your event list.

  • http://msdn.microsoft.com/en-us/library/ms132679.aspx
like image 31
JaredPar Avatar answered Sep 17 '22 18:09

JaredPar


You can't do this with List<T>. However, you can do it with ObservableCollection<T>. See ObservableCollection<T> Class.

like image 22
John Saunders Avatar answered Sep 20 '22 18:09

John Saunders