Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / July 2005

Tip: Looking for answers? Try searching our database.

Designing controls that have parameters in constructors

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex - 07 Jul 2005 20:25 GMT
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.

Signature

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

"Jeffrey Tan[MSFT]" - 08 Jul 2005 03:28 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

"Jeffrey Tan[MSFT]" - 12 Jul 2005 07:36 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.