Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to detect if some item was added in a queue let us say or a list. On the addition of that item i must get a message pop up. I have tried many things but dint work.

In simple words how to use event handling with collections.


Thanks and regards
Posted

Have you had a look at ObservableCollection[^] yet? It implements INotifyPropertyChanged and INotifyCollectionChanged out of the box. Looks like that's exactly what you are looking for.
 
Share this answer
 
Comments
BillWoodruff 1-Jan-14 4:54am    
+5 Yes !
Rahul VB 2-Jan-14 1:07am    
Hello Sir,
Yes i tried, but then i found out that it is available in .net frame work 4.0/4.5. So then i tried out BindingList which is available in 3.5. Now there are 2 events raised when a data is added to the binding list: AddingNew and ListChanged.

I am trying to figure out the difference between them, AddingNew event does not occur at all, i have tried it in a lot of situations. ListChanged event handler does not yield exact result required. Please advice.

Thanks a lot,
Rahul
dan!sh 2-Jan-14 1:10am    
ObservableCollection is available from .Net 3.0 framework. You should be able to use it.
 
Share this answer
 
The best way to approach this is to use .NET's 'ObservableCollection, and its INotify___ facilities.

Some people really struggle with mastering this, and I recommend you prepare yourself by studying the standard way custom events are defined in .NET: [^].

Here's an example of a Class that inherits from List<int> ... it's a generic collection of integers ... which implements a custom event:
C#
using System;
using System.Collections.Generic;

namespace Jan_1_2014_CollectionWithNotification
{
    public class CustomIntCollection : List<int>
    {
        // we want to expose the value of the integer
        // that is currently being added to the collection
        // that requires a custom EventArgs class
        // here this class is nested within the collection
        // ... it could be defined outside the collection
        public class NewIntAddEventArgs : EventArgs
        {
            public int intValue { get; set; }

            public NewIntAddEventArgs(int theInt) { intValue = theInt; }
        }

        // this is the EventHandler method that will be available
        // to "subscribe" to by consumers of this class
        public event EventHandler<NewIntAddEventArgs> NewIntAdded;

        public void Add(int newInt)
        {
            // invoke the custom Event
            OnRaiseNewIntAdded(new NewIntAddEventArgs(newInt));

            // let the base internal collection handle the 'Add
            base.Add(newInt);
        }

        // the custom Event code following standard MS C#
        // standards for raising a custom event
        protected virtual void OnRaiseNewIntAdded(NewIntAddEventArgs e)
        {
            EventHandler<NewIntAddEventArgs> handler = NewIntAdded;

            // fire the Event only if there are subscribers !
            if (handler != null) handler(this, e);
        }
    }
}
You might test the notification event facility of this class like this:
C#
//in some Form
CustomIntCollection cIntCollection = new CustomIntCollection();

private void SomeForm_Load(object sender, EventArgs e)
{
    // bind an EventHandler to this instance
    cIntCollection.NewIntAdded += cIntCollection_NewIntAdded;
}

// the EventHandler now bound to the instance of CustomIntCollection:
private void cIntCollection_NewIntAdded(object sender, CustomIntCollection.NewIntAddEventArgs e)
{
    Console.WriteLine("the integer added = " + e.intValue.ToString());
}

// somewhere inside a method in your code
// test adding an int, and range of ints to the instance:
cIntCollection.Add(100);
cIntCollection.AddRange(new List<int>{1,2,3,4});
The code to create custom Events that follows the MS Guidelines (yes, you could do it other ways), and use them, often seems quite strange to new students of C#, but you'll get used to it, and, I believe, you'll realize that what might appear to be a lot of "extra" work is related to allowing:

1. "safety" in code: preventing side-effects

2. creating structures/methods that can be over-ridden in inheriting classes.


[edit]Edit v3 [Nelek] First code block changed from xml to c# to improve color format and readability[/edit]
[edit]Edit v4 [Nelek] Explanation of changes in edit v3 and this line in request of poster[/edit]
 
Share this answer
 
v4
Comments
Nelek 1-Jan-14 12:55pm    
I have added the changes I made as you asked me :) Sorry about the changes in v3 without telling anything.
Rahul VB 2-Jan-14 2:50am    
Hello Sir,
Very nice, but a little tough to understand.

Thanks a lot
To add to D@nish answer if you are not using WPF I would suggest that you look at BindingList[^] with the INotifyPropertyChanged[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900