Microsoft KB Archive/813450

= How to make a UserControl object acts as a control container design-time by using Visual C# .NET or Visual C# 2005 =

Article ID: 813450

Article Last Modified on 11/13/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





For a Microsoft Visual Basic .NET version of this article, see 322222.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.ComponentModel

IN THIS TASK

 * SUMMARY


 * Overview
 * Create the UserControl as a Design-Time Control Container
 * Test the UserControl
 * REFERENCES



SUMMARY
This step-by-step article describes how to make a UserControl object act as a control container at design-time after you put the UserControl on a Windows Form. There may be situations where you want to drag a control to your UserControl. To do this, the UserControl must act as a control container.

back to the top

Overview
By default, a UserControl object can act as a control container only when you create the control. To make a UserControl host a constituent control after you put the UserControl on a Windows Form, you must change the default designer of the UserControl. To implement design-time services for a component, use the DesignerAttribute class of the System.ComponentModel namespace. The DesignerAttribute comes before the class declaration. Initialize the DesignerAttribute by passing the designerTypeName and the designerBaseType parameters.

designerTypeName is the fully qualified name of the designer type that provides design-time services. Pass the combination of the System.Windows.Forms.Design.ParentControlDesigner and the System.Design for the designerTypeName parameter. The ParentControlDesigner class extends design-time behavior for a UserControl.

designerBaseType is the name of the base class for the designer. The class that is used for the design-time services must implement the IDesigner interface.

back to the top

Create the UserControl as a Design-Time Control Container
 Create a new Visual C# .NET or Visual C# 2005 Windows Control Library project. To do this, follow these steps:  Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual C# Projects, and then click Windows Control Library under Templates.

Note In Visual Studio 2005, click Visual C# under Project Types.  Name the project ContainerUserControl. By default, UserControl1.cs is created. In Solution Explorer, right-click UserControl1.cs, and then click View Code.</li>  Add the following code to the Declarations section: using System.ComponentModel.Design; </li>  Apply the System.ComponentModel.DesignerAttribute attribute to the control as follows: [Designer(&quot;System.Windows.Forms.Design.ParentControlDesigner, System.Design&quot;, typeof(IDesigner))] public class UserControl1 : System.Windows.Forms.UserControl {

...

} </li> On the Build menu, click Build Solution.</li></ol>

back to the top

Test the UserControl
 Create a new Visual C# project. To do this, follow these steps:  Start Visual Studio .NET or Visual Studio 2005.</li> On the File menu, point to New, and then click Project.</li> Under Project Types, click Visual C# Projects, and then click Windows Application under Templates. By default, Form1.cs is created.

Note In Visual Studio 2005, click Visual C# under Project Types.

The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# 2005 adds one form to the project by default. This form is named Form1. The two files that represent the form are named Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by adding controls. For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/ms173077.aspx

</li></ol> </li> Drag UserControl1 from the toolbox (under Windows Forms) to Form1.cs.</li> Drag a Button control from the toolbox to UserControl1.</li> Notice that the UserControl1 behaves as control container for the Button control.</li></ol>

back to the top

<div class="references_section">