B
Bob Dankert
I have a custom control (inheriting from ListBox) which has a collection as
a property. This collection implements IList (and ICollection, IEnumerator)
and has the DesignerSerializationVisibility set to Content. The collection
consists of a custom class I created, which I also created a typeconverter
so I can use the Visual Studio designer to add items to my collection. When
I try to add items to my collection, I get an error that the constructor can
not be found.
I tried to use this code without the typeconverter and I had the problem
when I added items to my collection, the code would not be created by
designer in VS and therefore they would never be added.
I would appreciate any suggestions
My code for the collection property of the control is here:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public MyControls.MyListBoxColumnCollection Columns
{
get
{
return this.columns;
}
set
{
this.columns = value;
}
}
The code for the Column item is here (I am omiting the code for the
columncollection as this is very basic):
using System;
using System.Globalization;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
namespace MyControls
{
/// <summary>
/// Summary description for MyListBoxColumn.
/// </summary>
[TypeConverter(typeof(MyListBoxColumnConverter))]
public class MyListBoxColumn
{
private int width;
public MyListBoxColumn(int width)
{
this.width = width;
}
public int Width
{
get
{
return this.width;
}
set
{
this.width = value;
}
}
}
internal class MyListBoxColumnConverter : 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,CultureInfo
culture, object value, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
MyListBoxColumn col = (MyListBoxColumn)value;
//DockPosition dp = (DockPosition)value;
ConstructorInfo ctor = typeof(MyListBoxColumn).GetConstructor(new Type[]
{typeof(int)});
//ConstructorInfo ctor = typeof(DockPosition).GetConstructor(new Type[]
{typeof(int), typeof(int)});
return new InstanceDescriptor(ctor, new object[] {col.Width});
//return new InstanceDescriptor(ctor, new object[] {dp.Left,dp.Top});
}
return ConvertTo(context, culture, value, destinationType);
}
}
}
Thanks
Bob Dankert
a property. This collection implements IList (and ICollection, IEnumerator)
and has the DesignerSerializationVisibility set to Content. The collection
consists of a custom class I created, which I also created a typeconverter
so I can use the Visual Studio designer to add items to my collection. When
I try to add items to my collection, I get an error that the constructor can
not be found.
I tried to use this code without the typeconverter and I had the problem
when I added items to my collection, the code would not be created by
designer in VS and therefore they would never be added.
I would appreciate any suggestions
My code for the collection property of the control is here:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public MyControls.MyListBoxColumnCollection Columns
{
get
{
return this.columns;
}
set
{
this.columns = value;
}
}
The code for the Column item is here (I am omiting the code for the
columncollection as this is very basic):
using System;
using System.Globalization;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
namespace MyControls
{
/// <summary>
/// Summary description for MyListBoxColumn.
/// </summary>
[TypeConverter(typeof(MyListBoxColumnConverter))]
public class MyListBoxColumn
{
private int width;
public MyListBoxColumn(int width)
{
this.width = width;
}
public int Width
{
get
{
return this.width;
}
set
{
this.width = value;
}
}
}
internal class MyListBoxColumnConverter : 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,CultureInfo
culture, object value, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
MyListBoxColumn col = (MyListBoxColumn)value;
//DockPosition dp = (DockPosition)value;
ConstructorInfo ctor = typeof(MyListBoxColumn).GetConstructor(new Type[]
{typeof(int)});
//ConstructorInfo ctor = typeof(DockPosition).GetConstructor(new Type[]
{typeof(int), typeof(int)});
return new InstanceDescriptor(ctor, new object[] {col.Width});
//return new InstanceDescriptor(ctor, new object[] {dp.Left,dp.Top});
}
return ConvertTo(context, culture, value, destinationType);
}
}
}
Thanks
Bob Dankert