mercredi 25 février 2015

Editing a custom collection in windows forms designer

I am using a custom control in windows forms. When I edit the collection using UITypeEditor, the generated code is not proper.


Note: My collection is a custom collection implementing IList. The custom collection has 10 items in it by default.


Code used for using UITypeEditor in design time:



[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor)), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), TypeConverter(typeof(CollectionConverter))]
[Description("Collection of items.")]
public MyItemCollection Items
{
get
{
return m_Items;
}
}


The custom collection has 10 default items in it. After removing 5 items from the collection, generated code looks like


Generated code



this.myControl1.Items.Add(item1);
this.myControl1.Items.Add(item2);
this.myControl1.Items.Add(item3);
this.myControl1.Items.Add(item4);
this.myControl1.Items.Add(item5);


Expected code



this.myControl1.Items.Remove(removedIndex1);
this.myControl1.Items.Remove(removedIndex2);
this.myControl1.Items.Remove(removedIndex3);
this.myControl1.Items.Remove(removedIndex4);
this.myControl1.Items.Remove(removedIndex5);


Note: I'm using removedIndexonly for illustration


As you can see, the problem in generated code is it adds 5 new items to the existing collection instead of removing them.


Now, I'm stuck here not able to understand where the problem is whether this is the behavior of UITypeEditor or there are problems in my implementation of IList.


Aucun commentaire:

Enregistrer un commentaire