Ask a questionAsk a question
 

AnswerCollections Events

  • Tuesday, November 03, 2009 6:37 PMgborgesbr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi all!!!

    In my app there is a collection and I would like to fire an event when that collection changes. I would like to get the events if there is a modification on this collection such as: adding elemets, removing elements or changing elements attributes.

    The main question here is to know if is it possible to get this event in MultiChart class, in order to update the control which the user is editing.

    The second question is how can I get these events? Do I have to inherits from CollectionEditor? Which is the easiest way? If I need to inherits from Collection Editor, could you help me with some part of code?

    When the user edits the collection using the property grid, the parameterList is updated automatically in MultiChartClass.

    I am newbie in c#.

    I attach here some part of my code:

        public class MultiChart : ChartCommom, IXmlSerializable {
            ...
            private ParameterCollection parameterList = new ParameterCollection();
            ...
    
            [TypeConverter(typeof(ParameterCollectionConverter))]
            [Category("Parameter")]
            public ParameterCollection Parameters {
                get { return this.parameterList; }
            }
    }
    
        internal class ParameterCollectionConverter : ExpandableObjectConverter {
    
            public override object ConvertTo(ITypeDescriptorContext context, 
                System.Globalization.CultureInfo culture, object value, Type destType) {
                if (destType == typeof(string) && value is ParameterCollection) {
                    return "Patameters' data";
                }
                return base.ConvertTo(context, culture, value, destType);
            }
    
        }
    
      public class ParameterCollection : CollectionBase, ICustomTypeDescriptor {
    
            public ParameterCollection() {
            }
    
            public void Add ( ParameteronWidgetVo parameter ) {
                this.List.Add(parameter);
            }
    
            public void Remove(ParameteronWidgetVo parameter) {
                this.List.Remove(parameter);
            }
    
            public ParameteronWidgetVo this[int index] {
                get { return (ParameteronWidgetVo)this.List[index]; }
    
            }
    ...
    
           public PropertyDescriptorCollection GetProperties(Attribute[] attributes) {
                return GetProperties();
            }
    
            public PropertyDescriptorCollection GetProperties() {
                // Create a new collection object PropertyDescriptorCollection
    
                PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);
    
                // Iterate the list of parameters
    
                for (int i = 0; i < this.List.Count; i++) {
                    // For each parameter create a property descriptor 
                    // and add it to the 
                    // PropertyDescriptorCollection instance
    
                    ParameterCollectionPropertyDescriptor pd = new
                                  ParameterCollectionPropertyDescriptor(this, i);
                    pds.Add(pd);
                }
                return pds;
            }
    
    


        public class ParameterCollectionPropertyDescriptor : PropertyDescriptor {
    
            private ParameterCollection collection = null;
            private int index = -1;
    
            public ParameterCollectionPropertyDescriptor(ParameterCollection coll, int idx)
                : base("#" + idx.ToString(), null) {
                this.collection = coll;
                this.index = idx;
            }
    
            public override AttributeCollection Attributes {
                get {
                    return new AttributeCollection(null);
                }
            }
    ...
    }
    
        public class ParameteronWidgetVo {
    
            private ParameterVo f_prmtCode;
            public double f_offset;
            public double f_gain;
            public Color f_color;
            public string f_title;
            public object f_value = (double) 0;
    
            public ParameteronWidgetVo() {
                this.f_prmtCode = null;
                this.f_offset = 0;
                this.f_gain = 1;
                this.f_color = Color.Red;
                this.f_title = "";
            }
    
            [DisplayName("Identity")]
            public string pcode {
                get { return this.f_prmtCode.getParameterId(); }
            }
    
            [DisplayName("Title")]
            public string ptitle {
                get { return f_title; }
                set { f_title = value; AttributeChanged(this); }
            }
    
            [Category("Characteristics")]
            [DisplayName("Gain")]
            public double pgain {
                get { return f_gain; }
                set { f_gain = value; AttributeChanged(this); }
            }
    
            [Category("Characteristics")]
            [DisplayName("Offset")]
            public double poffset {
                get { return f_offset; }
                set { f_offset = value; AttributeChanged(this); }
            }
    
    ...
            public delegate void prmtonwdgvo(object sender);
            public static event prmtonwdgvo AttributeChanged;
    
    ...
    }
    



    Thank you in advance!!!
    Gustavo

Answers

  • Wednesday, November 04, 2009 7:19 PMGeert van Horrik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    In that case, you will have to implement the INotifyPropertyChanged interface. However, you can use this base class, which implements a lot of interfaces commonly used in data classes. Check for yourself whether you want a light weight class (then implement the INotifyPropertyChanged interface yourself) or a class with validation, property change notification, serialization, etc (then use the base class in the blog article).

    Btw: don't use the word attribute. An attribute is a decorator of a type or object or member (in your case Category and DisplayName). What you mean is a property, so use the right names.


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!

All Replies