views:

762

answers:

2

I have custom collection editor and I want to programmatically add items to it's list (collection) so they will could visible in a listbox. How I can do that? I know about CollectionEditor's AddItems method, but it takes collection object as a parameter, but I cannot figure out a way to get CollectionEditor's inner list object... :/

[update] Ugh.. the proper method name is 'SetItems' [/update]

[update 2] Source code of what I'm trying to do...

public class MyCollectionEditor : CollectionEditor
{
     private Type m_itemType = null;

     public MyCollectionEditor(Type type)
      : base(type)
     {
      m_itemType = type;
     }

     protected override CollectionForm CreateCollectionForm()
     {
      Button buttonLoadItem = new Button();
      buttonLoadItem.Text = "Load from DB";
      buttonLoadItem.Click += new EventHandler(ButtonLoadItem_Click);

      m_collectionForm = base.CreateCollectionForm();

      TableLayoutPanel panel1 = m_collectionForm.Controls[0] as TableLayoutPanel;
      TableLayoutPanel panel2 = panel1.Controls[1] as TableLayoutPanel;
      panel2.Controls.Add(buttonLoadItem);

      return m_collectionForm;
     }

     private void ButtonLoadItem_Click(object sender, EventArgs e)
     {
      if (m_itemType.Equals(typeof(MyCustomCollection)))
      {    
       MyCustomItem item = ...load from DB...

       //definition: SetItems(object editValue, object[] value);
       SetItems( -> what goes here?! <- , new object[] { item });
      }
     }
}

[/update 2]

A: 

I may be misunderstanding your question but dont you have to define you own collection first ? and then decorate it with the EditorAttribute

[EditorAttribute(typeof(System.ComponentModel.Design.CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]

76mel
Yes, you are right. I have my collection, I derived CollectionEditor class and decorated my collection as you suggested. What I want to achive is - within CollectionEditor I added a button to load item from DB (next to original buttons to add and delete), after I handle loading I want to add this item to list. There is a method to 'set' items (in CollectionEditor), but it's necessary to pass actual list of objects, which is protected and thus inaccessible.
stavo
A: 

I've found solution thanks to .NET Reflector and reflection mechanism. Instead of using SetItems method I'm invoking private method of CollectionForm: private void AddItems(IList instances), like this:

MethodInfo methodInfo = m_collectionForm.GetType().GetMethod("AddItems", BindingFlags.NonPublic | BindingFlags.Instance);
methodInfo.Invoke(m_collectionForm, new object[] { /* my items here */ });

PS. See the rest of code above...

stavo