Strange Behavior

M

mehdi_mousavi

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:

////////////////////////////////////////AddressCollectionEditor
public class AddressCollectionEditor : CollectionEditor
{
public AddressCollectionEditor(Type type)
: base(type){}

protected override bool CanSelectMultipleInstances(){return
false;}

protected override Type CreateCollectionItemType(){return
typeof(Address);}
}

////////////////////////////////////////AddressTypeConverter
[Serializable]
public class AddressTypeConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext
context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
return true;

return base.CanConvertTo(context, destinationType);
}

public override object ConvertTo(ITypeDescriptorContext
context, System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
Address addr = value as Address;
if (addr == null)
return new
InstanceDescriptor(typeof(Address).GetConstructor(System.Type.EmptyTypes),
null);

return new
InstanceDescriptor(typeof(Address).GetConstructor(new Type[] {
typeof(string), typeof(string), typeof(string), typeof(string),
typeof(string), typeof(string), typeof(string) }),

new string[] { addr.Type, addr.Street1, addr.Street2, addr.City,
addr.StateProvince, addr.ZipPostalCode, addr.CountryRegion });
}

return base.ConvertTo(context, culture, value,
destinationType);
}
}

////////////////////////////////////////AddressCtrl
public partial class AddressCtrl : UserControl
{
//removed for clarity....

[Category("Misc"), Description("Get a collection of
addresses.")]
[Editor(typeof(AddressCollectionEditor), typeof(UITypeEditor))]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public AddressCollection Addresses
{
get { return m_coll; }
}
}

////////////////////////////////////////Address
[Serializable]
[TypeConverter(typeof(AddressTypeConverter))]
public class Address : ICloneable
{
//removed for clarity....


private string m_strType;
private string m_strStreet1, m_strStreet2;
private string m_strCity, m_strStateProvince;
private string m_strZipPostalCode, m_strCountryRegion;

public Address()
: this("Unknown", string.Empty, string.Empty, string.Empty,
string.Empty, string.Empty, string.Empty)
{
}

public Address(string strType, string strStreet1, string
strStreet2, string strCity, string strStateProvince, string
strZipPostalCode, string strCountryRegion)
{
}

//All the member variables are exposed to the outer world
through some
//accessors (get/set)....
}

Everything works fine and the following code is generated in the
InitializeComponent:

this.addressCtrl1.Addresses.AddRange(new Address[] {
new Address("Business", "", "", "", "", "", ""),
new Address("Home", "", "", "", "", "", ""),
new Address("Shipping", "", "", "", "", "", ""),
new Address("Billing", "", "", "", "", "", "")});

However, when I try to remove a given item, the InitializeComponent
looks like this:

this.addressCtrl1.Addresses.AddRange(new Address[] {
((Address)(new Address())),
((Address)(new Address())),
((Address)(new Address())),
((Address)(new Address()))});

Would you please let me know what I'm doing wrong?

Thank you for your time,
Mehdi
 
B

Bruce Wood

You might try posing this question in
microsoft.public.dotnet.framework.windowsforms.controls.
 

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