Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. How to programmatically add items to CollectionEditor's list. [modified]

How to programmatically add items to CollectionEditor's list. [modified]

Scheduled Pinned Locked Moved Windows Forms
questiondatabasedesigntutorialannouncement
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ravtos
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • R ravtos

      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

      R Offline
      R Offline
      ravtos
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups