Hi folks,
I've create a UserControl that could return a property of type
AddressCollection - a collection of Address classes (derived from
CollectionBase). I've also created an AddressTypeConverter that's being
derived from TypeConverter as well as an AddressCollectionEditor that's
being derived from CollectionEditor as follows:
public class AddressCollectionEditor : CollectionEditor<br />
{<br />
public AddressCollectionEditor(Type type)<br />
: base(type){}<br />
<br />
protected override bool CanSelectMultipleInstances(){return<br />
false;}<br />
<br />
protected override Type CreateCollectionItemType(){return<br />
typeof(Address);}<br />
}<br />
<br />
[Serializable]<br />
public class AddressTypeConverter : TypeConverter<br />
{<br />
public override bool CanConvertTo(ITypeDescriptorContext<br />
context, Type destinationType)<br />
{<br />
if (destinationType == typeof(InstanceDescriptor))<br />
return true;<br />
<br />
return base.CanConvertTo(context, destinationType);<br />
}<br />
<br />
public override object ConvertTo(ITypeDescriptorContext<br />
context, System.Globalization.CultureInfo culture, object value, Type<br />
destinationType)<br />
{<br />
if (destinationType == typeof(InstanceDescriptor))<br />
{<br />
Address addr = value as Address;<br />
if (addr == null)<br />
return new<br />
InstanceDescriptor(typeof(Address).GetConstructor(System.Type.EmptyTypes),<br />
null);<br />
<br />
return new<br />
InstanceDescriptor(typeof(Address).GetConstructor(new Type[] {<br />
typeof(string), typeof(string), typeof(string), typeof(string),<br />
typeof(string), typeof(string), typeof(string) }),<br />
<br />
new string[] { addr.Type, addr.Street1, addr.Street2, addr.City,<br />
addr.StateProvince, addr.ZipPostalCode, addr.CountryRegion });<br />
}<br />
<br />
return base.ConvertTo(context, culture, value,<br />
destinationType);<br />
}<br />
}<br />
<br />
public partial class AddressCtrl : UserControl<br />
{<br />
<br />
[Category("Misc"), Description("Get a collection of<br />
addresses.")]<br />
[Editor(typeof(AddressCollectionEditor), typeof(UITypeEditor))]<br />
<br />
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]<br />
public AddressCollection Addresses<br />
{<br />
get { return m_coll; }<br />
}<br />
<br />
}<br />
<br />
[Serializable]<br />
[TypeConverter(typeof(AddressTypeConverter))]<br />
public class Address : ICloneable<br />
{<br />
<br />
private string m_strType;<br />
private string m_strStreet1, m_strStreet2;<br />
private string m_strCity, m_strStateProvince;<br />
private string m_strZipPostalCode, m_strCountryRegion;<br />
<br />
public Address()<br />
: this("Unknown", string.Empty, string.Empty, string.Empty,<br />
string.Empty, string.Empty, string.Empty)<br />
{<br />
}<br />
<br />
public Address(string strType, string strStreet1, string<br />
strStreet2, string strCity, string strStateProvince, string<br />
strZipPostalCode, string strCountryRegion)<br />
{<br />
}<br />
<br />
through some<br />
}
Everything works fine and the following code is generated in the
InitializeComponent:
<br />
this.addressCtrl1.Addresses.AddRange(new Address[] {<br />
new Address("Business", "", "", "", "", "", ""),<br />
new Address("Home", "", "", "", "", "", ""),<br />
new Address("Shipping", "", "", "", "", "", ""),<br />
new Address("Billing", "", "", "", "", "", "")});<br />
However, when I try to remove a given item, the InitializeComponent
looks like this:
<br />
this.addressCtrl1.Addresses.AddRange(new Address[] {<br />
((Address)(new Address())),<br />
((Address)(new Address())),<br />
((Address)(new Address())),<br />
((Address)(new Address()))});<br />
Would you please let me know what I'm doing wrong?
Thank you for your time,
Mehdi
|