Amal
Amal

Reputation: 586

How to enable add and delete button of collectioneditor

I have derived a collection editor for my collection class. but the add and delete button in the collection editor is not enabled so that i could not add or delete a instance in collection through designer.

Here is the code i have used,

[EditorAttribute(typeof(MyCollectionEditor), typeof(UITypeEditor))]
public MyCollection : IDisposable, ICollection
{  
    List<MyClass> list= new List<MyClass>();

    public Add(MyClass myclass)
    {

    }

    ......


 }


 public class MyCollectionEditor : CollectionEditor
 {
    public MyCollectionEditor ()
       : base(typeof(MyCollection))
    {
    }


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

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        return base.EditValue(context, provider, value);
    }
 }

Could anyone please let me know, how to enable add, delete buttons in collection editor and make it work for my collection?

Regards,

Upvotes: 1

Views: 1022

Answers (1)

DotNet Developer
DotNet Developer

Reputation: 3018

Try this:

 public class MyCollectionEditor : CollectionEditor
    {
        public MyCollectionEditor()
            : base(typeof(MyCollection))
        {
        }

        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm form = base.CreateCollectionForm();
            Type type = form.GetType();
            PropertyInfo propertyInfo = type.GetProperty("CollectionEditable", BindingFlags.Instance | BindingFlags.NonPublic);
            propertyInfo.SetValue(form, true);
            return form;
        }

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

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            return base.EditValue(context, provider, value);
        }
    }

This should enable "Add" and "Remove" buttons.

Upvotes: 1

Related Questions