Hack Forums
[C#] Collection Editor for my custom collection - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Collection Editor for my custom collection (/showthread.php?tid=5047775)



Collection Editor for my custom collection - aleeeeeee - Nov 6, 2015

Hi i have gray button in collection editor
i use propertygrid
[Image: rB026zG.png]
this are the code
Code
internal class RulesCollection : IList<Rule>
    {
        private List<Rule> _rules;

        public RulesCollection()
        {
            _rules = new List<Rule>();
        }

        public IEnumerator<Rule> GetEnumerator()
        {
            return _rules.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(Rule item)
        {
            var thereIsFirst = _rules.FirstOrDefault(r => r.Type == RuleType.First) != null;
            if (thereIsFirst && item.Type == RuleType.First)
                throw new Exception("\nCan be only one first rule.\nCreate new set.");
            _rules.Add(item);

        }

        public void Clear()
        {
            _rules.Clear();
        }

        public bool Contains(Rule item)
        {
            return _rules.Contains(item);
        }

        public void CopyTo(Rule[] array, int arrayIndex)
        {
            _rules.CopyTo(array, arrayIndex);
        }

        public bool Remove(Rule item)
        {
            return _rules.Remove(item);
        }

        public int Count
        {
            get { return _rules.Count; }
        }

        public bool IsReadOnly { get; } = false;

        public int IndexOf(Rule item)
        {
            return _rules.IndexOf(item);
        }

        public void Insert(int index, Rule item)
        {
            _rules.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _rules.RemoveAt(index);
        }

        public Rule this[int index]
        {
            get { return _rules[index]; }
            set { _rules[index] = value; }
        }
    }

Code
internal class RulesCollectionEditor : CollectionEditor
    {
        public RulesCollectionEditor(Type type) : base(type)
        {
        }

        protected override Type CreateCollectionItemType()
        {
            return typeof (Rule);
        }

        protected override Type[] CreateNewItemTypes()
        {
            return new[] {typeof (Rule)};
        }        

    }

Code
class RuleSet
    {
        public string Name { get; set; }


        private RulesCollection _rules;
        
        [Editor(typeof (RulesCollectionEditor), typeof (UITypeEditor))]
        public RulesCollection Rules { get { return _rules; } }

        public RuleSet()
        {
            _rules = new RulesCollection();
        }

    }