Something about CollectionEditor

Today I wrote a simple example which shows how we can use CollectionEditor of the .NET. In addition to this I also made a simple example which shows how to implement custom CollectionEditor which allows us to add more than one item to the collection.

Download Example Sources

Download Example

public class ExampleComponent : Component{
       ArrayListCollection _ArrayListItems = new ArrayListCollection();
       List<Button> _ListItems = new List<Button>();
       List<Control> _MultipleItems = new List<Control>();

 
      public ExampleComponent()
       { }

       [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
       [Description("Contains only Control objects in an ArrayList")]
       [Category("Behaviour")]
       public ArrayListCollection ArrayListItems
       {
             get { return _ArrayListItems; }
       }

       [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
       [Description("Contains only Button objects in a List<Button> collection")]
       [Category("Behaviour")]
       public List<Button> ListItems
       {
             get { return _ListItems; }
       }

       // custom editor attribute
       [Editor(typeof(CustomCollectionEditor), typeof(UITypeEditor))]
       [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
       [Description("Contains ListView, Button and CheckBox items in a List<Control> collection")]
       [Category("Behaviour")]
       public List<Control> MultipleItems
       {
             get { return _MultipleItems; }
       }

       public class ArrayListCollection : ArrayList
       {
             // CollectionEditor searches collection type's Index property and finds its property type.
             // It uses this type to add new items
             public new Control this[int index]
             {
                    get { return base[index] as Control; }
             }
       }

       // Custom collection editor.
       // by using this editor we can add ListView, Button and
       // CheckBoxes to the same collection
       public class CustomCollectionEditor : CollectionEditor
       {
             public CustomCollectionEditor()
                    : base(typeof(List<Control>))
             {}

             // override this method if you have to do custom initializing
             // operations. For example, if you have to use a constructor which
             // takes arguments you can create your own items here ...
             // or may be you have to do some initializing operations ...
             protected override object CreateInstance(Type itemType)
             {
                    if (itemType == typeof(CheckBox))
                    {
                           CheckBox checkbox = new CheckBox();
                           checkbox.Text = "My Text, hede";
                           return checkbox;
                    }
                    Control control = base.CreateInstance(itemType) as Control;
                    control.Text = "Other, hede";
                    return control;
             }

             // if you want to use a custom collection editor form,
             // you have to override this method and return your form here
             protected override CollectionEditor.CollectionForm CreateCollectionForm()
             {
                    return base.CreateCollectionForm();
             }

             // here you can return a text which will be appeared in the list
             // for the given item
             protected override string GetDisplayText(object value)
             {
                    Control control = value as Control;
                    return string.Format("{0} - {1}", control.GetType().Name, control.Text);
             }

             // we allow 3 types can be added to our collection
             protected override Type[] CreateNewItemTypes()
             {
                    return new Type[] { typeof(ListView), typeof(Button), typeof(CheckBox) };
             }
       }

}


Related posts

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

November 20. 2008 01:53 AM


Search

Calendar

<<  November 2008  >>
MonTueWedThuFriSatSun
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
View posts in large calendar

Disclaimer

© 2007 - 2008
Ozcan DEGIRMENCI
All rights reserved. The content can be used elsewhere given that the source is properly acknowledged.