HighTechTalks DotNet Forums  

Designing controls that have parameters in constructors

Dotnet Framework (WinForms DesignTime) microsoft.public.dotnet.framework.windowsforms.designtime


Discuss Designing controls that have parameters in constructors in the Dotnet Framework (WinForms DesignTime) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Alex
 
Posts: n/a

Default Designing controls that have parameters in constructors - 07-07-2005 , 02:25 PM






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.

--
Address email to user "response" at domain "alexoren" with suffix "com"

Reply With Quote
  #2  
Old   
AT
 
Posts: n/a

Default RE: Designing controls that have parameters in constructors - 07-07-2005 , 09:28 PM






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.UserControlConve rter))]
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.


Reply With Quote
  #3  
Old   
AT
 
Posts: n/a

Default RE: Designing controls that have parameters in constructors - 07-12-2005 , 01:37 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.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.