PropertyGrid, CollectionEditor ArrayList?

G

Gert

Hi

I have two classes, Trade and TradeAssets. I create a form with a
PropertyGrid and set the trade object to the SelectedObject object of
the PropertyGrid.

As I understand the collection editor picks up the type of the Item
property of the collection to determine what objects to create when
the add button is pressed.
:
:
oTrade = new Trade();
propertyGrid1.SelectedObject = oTrade;
:
:
[Serializable()]
public class Trade
{
private double _Quantity;
private double _Price;
private double _Value;
private ArrayList _TradeAssetsList;
//private TradeAssets[] _TradeAssetsList;

[Description("Number of shares"),Category("General")]
public double Quantity
{
get { return _Quantity; }
set { _Quantity = value;}
}
[Description("The price of the share"),Category("General")]
public double Price
{
get { return _Price; }
set { _Price = value;}
}
[Description("Value of the shares"),Category("General")]
public double Value
{
get { return _Value; }
set { _Value = value;}
}

[XmlArrayItem(ElementName= "TradeAssets", Type =
typeof(TradeAssets))]
public ArrayList TradeAssetsList
{
get { return _TradeAssetsList; }
set { _TradeAssetsList = value;}
}

// [XmlArrayItem(ElementName= "TradeAssets", Type =
typeof(TradeAssets))]
// public CTradeAssets[] TradeAssetsList
// {
// get { return _TradeAssetsList; }
// set { _TradeAssetsList = value;}
// }

public Trade()
{
TradeAssetsList = new ArrayList();
//TradeAssetsList.Add(new TradeAssets());
}
}

[Serializable()]
public class TradeAssets
{
private double _Quantity;

[Description("Number of shares"),Category("General")]
public double Quantity
{
get { return _Quantity; }
set { _Quantity = value;}
}

public TradeAssets()
{
}
}


Q: If I declare 'private TradeAssets[] _TradeAssetsList;' the
collection editor works fine. If I declare 'private ArrayList
_TradeAssetsList;' is creates System.Objects when I press the add
button. I would have thought that if I say the typeof the ArrayList is
of type TradeAssets, '[XmlArrayItem(ElementName= "TradeAssets", Type =
typeof(TradeAssets))]', the collection editor would pick it up?
I wouldn't like to write a collection class for every object or is
this the only way
public sealed class TradeAssetsCollection : CollectionBase
{
public TradeAssets this[int index]
{
get
{
return(TradeAssets)(List[index]);
}
set
{
List[index] = value;
}
}
}

Thanks
Gert
 
G

Gert

Tanks raz

I had to override the EditValue and CreateCollectionItemType to get it
working. This 'MyCollectionEditor' will be able to edit any ArrayList
with the attribute '[XmlArrayItem(ElementName= "TradeAssets", Type =
typeof(TradeAssets))]' set. It gets the type from the Attribute


public class MyCollectionEditor :
System.ComponentModel.Design.CollectionEditor
{
private Type mType;
public MyCollectionEditor(Type type) : base(type)
{
}

public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
object[] oAttr;
XmlArrayItemAttribute xmlAttr;
PropertyInfo oProperty = null;

Type oType = context.Instance.GetType();

PropertyInfo[] oPropertyInfo = oType.GetProperties();
for (int i = 0; i < oPropertyInfo.Length; i++)
{
oProperty = oPropertyInfo;

if (context.PropertyDescriptor.Name == oProperty.Name)
{
oAttr =
oProperty.GetCustomAttributes(typeof(XmlArrayItemAttribute),false);
if (oAttr.Length > 0)
{
xmlAttr = oAttr[0] as XmlArrayItemAttribute;
mType = xmlAttr.Type;
}
}
}
return base.EditValue (context, provider, value);
}

protected override System.Type[] CreateNewItemTypes()
{
return new Type[] {mType};
}
protected override Type CreateCollectionItemType()
{
return mType;
}
}

Gert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top