Hi Alex,
Thanks for your post.
Based on my understanding, you want to force VS.net designer to use certain
parameterized constructor of your control when droping it to the Form
designer.
Normally, VS.net code generator will query InstanceDescriptor to choose the
constructor. So we can attach a TypeConverter to our control, then in the
ConvertTo method, we can explicitly specify a constructor to use. I have
writen a simple sample like this:
[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);
}
}
}
When I drop this usercontrol to the designer, it will generate code like
this:
private void InitializeComponent()
{
this.userControl11 = new CollectionSerializeTest.UserControl1(1);
...
}
For more information, you can refer "Generating Code for Custom Types"
section in Shawn Burke's wonderful article below:
"Customizing Code Generation in the .NET Framework Visual Designers"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/custcodegen.asp?frame=true
Hope this helps
=============================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.