3 Replies - 1666 Views - Last Post: 30 December 2010 - 11:37 AM Rate Topic: -----

#1 Guest_Guest*


Reputation:

writing a custom list<t> event

Posted 30 December 2010 - 11:16 AM

so i want to update a combobox when the count of a list<t> changes. the list will be populated with objects from class C which all have 6 properties.

so i want my combobox to show 1 of the attributes of the item C in the list.

here is what i have so far:
        public List<C> PresetList = new List<C>();
        private int CCount;

        public delegate void CListItemsChanged(int Count);
        public event CListItemsChanged ItemsChanged;

        private void GetListItemCount(int Count)
        {
            CCount = CList.Count;
        }

        public formMain()
        {
            this.CCount = this.CList.Count;
            InitializeComponent();
        }




Right now I just want to modify the int variable to figure this out. any suggestions/ i read the custom events tutorial but i keep gettign told i cant apply the event to the list.

Is This A Good Question/Topic? 0

Replies To: writing a custom list<t> event

#2 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 217
  • Joined: 18-May 10

Re: writing a custom list<t> event

Posted 30 December 2010 - 11:30 AM

You can't add an event to an existing class without modifying it. The List<T> class does not know when to raise the event.
You would rather need to extend the List<T> class to create a new class the can raise events:

public class EventList<T> : List<T>
    {
        public delegate void CListItemsChanged(int count);
        public event CListItemsChanged ItemsChanged;

        public new void Add(T item)
        {
            base.Add(item);
            if (ItemsChanged != null) ItemsChanged(this.Count);
        }

        public new void Remove(T item)
        {
            base.Remove(item);
            if (ItemsChanged != null) ItemsChanged(this.Count);
        }
    }


Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,061
  • Joined: 02-June 10

Re: writing a custom list<t> event

Posted 30 December 2010 - 11:32 AM

A List<> does not have an event for when the number of items changes.
You don't just apply an event to a pre-existing something.
You have to make an object/class and have it raise that event.
No place in your code do I see you actually raising the ItemsChanged event.

I don't know which tutorial you are referring to, as the tutorial you read. But maybe these will help.

Quick and easy custom events
Bulding an application - Part 1
Was This Post Helpful? 1
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4558
  • View blog
  • Posts: 7,970
  • Joined: 08-June 10

Re: writing a custom list<t> event

Posted 30 December 2010 - 11:37 AM

Quote

but i keep gettign told i cant apply the event to the list


There's a reason for that: it's because you can't.

You can't apply custom events to objects. You can apply them to classes you're creating.

However, I have just the thing for you:
ObservableCollection<T>.

This acts in many ways like a list. You;ll have to add:
using System.Collections.ObjectModel; to the top of your code to use this.

The major difference between this and a list is that ObservableCollection exposes exactly the event you want: Collectionchanged. This event will trigger every time something is added or removed from the list. So just use one of these and you'll be set.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1