design time instantiation


I have written a Component that I have included in the toolbox so I can drag it into a Form in design time. If I have a constructor in my Component with no parameters, the auto generated code in my Form calls this constructor. And if my Component constructor has aSystem.ComponentModel.IContainer as a in parameter, the auto generated code calls this constructor instead. Is it possible to write another constructor for my Component which both have aSystem.ComponentModel.IContainer and a System.Windows.Form as in parameters, and have the auto generated code pass the calling Form as the second parameter, and pass the same parameter as above as the first parameter?
[719 byte] By [Pepp] at [2007-12-16]
# 1

Non-component objects can use an InstanceDescriptor, created via a custom TypeConverter, to describe which constructor should be used. It was my understanding that this method was not supported by IComponent-derived types, according to some posts by Microsoft employees.

But recently a post on the design-time newsgroup included some code to do something similar -- use a different constructor, with different arguments. The idea in this case was to generate this construction code in InitializeComponent, which takes an integer parameter:

this.userControl11 = new CollectionSerializeTest.UserControl1(1);

Here is the sample:


[TypeConverter(typeof(UserControl1.UserControlConverter))]
public class UserControl1 : System.Windows.Forms.UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitializeComponent call

}

private int test;
public UserControl1(int tb)
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
test=tb;
}

public class UserControlConverter: 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))
{
if(value is UserControl1)
{
UserControl1 uc=value as UserControl1;
ConstructorInfo ci=typeof(UserControl1).GetConstructor(new
Type[]{typeof(int)});
if (ci != null)
{
return new
InstanceDescriptor(
ci,
new object[]{1});
}
}
}
return base.ConvertTo (context, culture, value, destinationType);
}
}
}


If you have any questions about this code, please read this article first:
"Customizing Code Generation in the .NET Framework Visual Designers"

Regards,
Frank Hileman
http://www.vgdotnet.com

FrankHileman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...