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. C#
  4. Custom collection and Properties window

Custom collection and Properties window

Scheduled Pinned Locked Moved C#
help
5 Posts 2 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.
  • M Offline
    M Offline
    Maciej Pirog
    wrote on last edited by
    #1

    Hey! I've got a problem with my custom collection! The collection is used as a type for a property in my control. When I put the control to the form in the Designer, and Click the (...) button at that property, new window is shown. I can add and remove elements in collection. But when I click the Add button, the type of new element is System.Object, not the type of elements of my collection. I want to do sth like ListView.ListViewItemCollection, which is editable through the designer! Thanks!

    Ñ There is only one MP Ð

    L 1 Reply Last reply
    0
    • M Maciej Pirog

      Hey! I've got a problem with my custom collection! The collection is used as a type for a property in my control. When I put the control to the form in the Designer, and Click the (...) button at that property, new window is shown. I can add and remove elements in collection. But when I click the Add button, the type of new element is System.Object, not the type of elements of my collection. I want to do sth like ListView.ListViewItemCollection, which is editable through the designer! Thanks!

      Ñ There is only one MP Ð

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Hi I assume you are inheriting from ArrayList. You should inherite from CollectionBase and expose the protected List 's methods as it needs like: public virtual void Add(Item value) { this.List.Add(value); } I did this in a few minutes and everything works 100%. IMPORTANT: Like all designer related stuff, the custom (really a strongly typed) collection as well as the objects inserted into the collection MUST have default constructors. Cheers :) Give them a chance! Do it for the kittens, dear God, the kittens!

      M 1 Reply Last reply
      0
      • L leppie

        Hi I assume you are inheriting from ArrayList. You should inherite from CollectionBase and expose the protected List 's methods as it needs like: public virtual void Add(Item value) { this.List.Add(value); } I did this in a few minutes and everything works 100%. IMPORTANT: Like all designer related stuff, the custom (really a strongly typed) collection as well as the objects inserted into the collection MUST have default constructors. Cheers :) Give them a chance! Do it for the kittens, dear God, the kittens!

        M Offline
        M Offline
        Maciej Pirog
        wrote on last edited by
        #3

        Strange! Take a look at my code:

        public class R
        {
        public R(){a=10;b=56;}
        int a, b;
        public int A{
        get{return a;}
        set{a = value;}}
        public int B
        {
        get{return b;}
        set{b = value;}
        }
        }

        public class Col: CollectionBase
        {
        public Col(){}
        public virtual void Add(R valueT)
        {
        this.List.Add(valueT);
        }
        // ...
        }

        But the designer still adds the System.Object item, not the R item. What's wrong?

        Ñ There is only one MP Ð

        L 1 Reply Last reply
        0
        • M Maciej Pirog

          Strange! Take a look at my code:

          public class R
          {
          public R(){a=10;b=56;}
          int a, b;
          public int A{
          get{return a;}
          set{a = value;}}
          public int B
          {
          get{return b;}
          set{b = value;}
          }
          }

          public class Col: CollectionBase
          {
          public Col(){}
          public virtual void Add(R valueT)
          {
          this.List.Add(valueT);
          }
          // ...
          }

          But the designer still adds the System.Object item, not the R item. What's wrong?

          Ñ There is only one MP Ð

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Hi MP Actually I realised today that you just need the indexer :) CollectionEditor from MSDN: Notes to Inheritors: This editor can edit collections that have an Item property. The editor can determine the type of the collection from the Item property, if it exists. If the collection does not have this property, or if you want to provide collections of more than one type, you can override certain protected members of this class to customize the editor to support other types of collections. so just do this:

          public MyItem this[int i]
          {
          get {return (MyItem) base.List[i];}
          set {base.List[i] = value;}
          }

          Cheers :) Give them a chance! Do it for the kittens, dear God, the kittens!

          M 1 Reply Last reply
          0
          • L leppie

            Hi MP Actually I realised today that you just need the indexer :) CollectionEditor from MSDN: Notes to Inheritors: This editor can edit collections that have an Item property. The editor can determine the type of the collection from the Item property, if it exists. If the collection does not have this property, or if you want to provide collections of more than one type, you can override certain protected members of this class to customize the editor to support other types of collections. so just do this:

            public MyItem this[int i]
            {
            get {return (MyItem) base.List[i];}
            set {base.List[i] = value;}
            }

            Cheers :) Give them a chance! Do it for the kittens, dear God, the kittens!

            M Offline
            M Offline
            Maciej Pirog
            wrote on last edited by
            #5

            Thanks! :-D Now it's working!

            Ñ There is only one MP Ð

            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