Custom collection and Properties window
-
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 theAdd
button, the type of new element isSystem.Object
, not the type of elements of my collection. I want to do sth likeListView.ListViewItemCollection
, which is editable through the designer! Thanks!Ñ There is only one MP Ð
-
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 theAdd
button, the type of new element isSystem.Object
, not the type of elements of my collection. I want to do sth likeListView.ListViewItemCollection
, which is editable through the designer! Thanks!Ñ There is only one MP Ð
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!
-
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!
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 Ð
-
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 Ð
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!
-
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!
Thanks! :-D Now it's working!
Ñ There is only one MP Ð