all groups > dotnet windows forms designtime > july 2005 >
You're in the

dotnet windows forms designtime

group:

Designing controls that have parameters in constructors


Designing controls that have parameters in constructors Alex
7/7/2005 3:25:46 PM
dotnet windows forms designtime:
Hello,

[ Using C# in VS 2003 ]

I want to have a control with a constructor that accepts several =
parameters.

Unfortunately, the Visual Studio designers insist on parameterless =
constructors.
The problem is, exposing a parameterless constructor may create the =
object in an inconsistent state.

Is it possible to use the designer on controls that do not have =
parameterless constructors?

Thank you.

Best wishes,
Alex.

--=20
RE: Designing controls that have parameters in constructors v-jetan NO[at]SPAM online.microsoft.com (
7/8/2005 12:00:00 AM
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.
RE: Designing controls that have parameters in constructors v-jetan NO[at]SPAM online.microsoft.com (
7/12/2005 6:37:30 AM
Hi Alex,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, thanks.

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.
AddThis Social Bookmark Button