How to programmatically add items to CollectionEditor's list. [modified]
-
Hello! I have a custom collection editor which inherits after CollectionEditor class (System.ComponentModel.Design)and I want to programmatically add items to it's collection (listbox). There is a method 'AddItems' which takes a collection object and items to add, but I cannot figure out what collection object I should pass to it.. So my question is, how I can get to CollectionEditor's inner item's list? [update] Ugh.. proper method name is 'SetItems' [/update] [update 2 - source code]
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]
A man has got to know his limitations. Harry Callahan
modified on Tuesday, July 28, 2009 6:34 AM
-
Hello! I have a custom collection editor which inherits after CollectionEditor class (System.ComponentModel.Design)and I want to programmatically add items to it's collection (listbox). There is a method 'AddItems' which takes a collection object and items to add, but I cannot figure out what collection object I should pass to it.. So my question is, how I can get to CollectionEditor's inner item's list? [update] Ugh.. proper method name is 'SetItems' [/update] [update 2 - source code]
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]
A man has got to know his limitations. Harry Callahan
modified on Tuesday, July 28, 2009 6:34 AM
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 in previous post...
A man has got to know his limitations. Harry Callahan