The default CollectionEditor will allow your user to add objects of type returned by your collection’s indexer method.
You can make this CollectionEditor allow your user to pick the type of object to add by deriving from CollectionEditor and making the change as shown below. This will introduce a drop-down in the editor’s ‘Add’ button to allow the user to pick the type he wants to add.
public class CustomCollectionEditor : System.ComponentModel.Design.CollectionEditor
{
private Type[] types;
public CustomCollectionEditor(Type type)
: base(type)
{
types = new Type[]{typeof(ItemType1), typeof(ItemType2), typeof(ItemType3),
typeof(ItemType4)};
}
// Return the types that you want to allow the user to add into your collection.
protected override Type[] CreateNewItemTypes()
{
return types;
}
}
[Editor(typeof(CustomCollectionEditor), typeof(UITypeEditor))]
public class CustomCollection : IList
{
...
}
Share with