Microsoft KB Archive/322222

= How to make a UserControl object behave as a Design-Time control container by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 322222

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q322222





For a Microsoft Visual C# .NET version of this article, see 813450.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * 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 behave as a control container at design-time after you add the UserControl object to a Windows Form. There may be situations where you want to drag a control to your UserControl object. To permit this, the UserControl object must behave as a control container.

back to the top

Overview
By default, a UserControl object behaves as a control container only while you are creating the control. To make a UserControl object host a constituent control after you add the UserControl object to a Windows Form, you must change the default designer of the UserControl object. To implement design-time services for a component, use the DesignerAttribute class of the System.ComponentModel namespace. The DesignerAttribute class appears before the class declaration. Initialize the DesignerAttribute by passing the designerTypeName parameter and the designerBaseType parameter.
 * designerTypeName is the fully qualified name of the designer type that provides design-time services. Pass the combination of System.Windows.Forms.Design.ParentControlDesigner and System.Design for the designerTypeName parameter. The ParentControlDesigner class extends design-time behavior for a UserControl object.
 * 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
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a new Windows Control Library project by using Visual Basic .NET or Visual Basic 2005. Name the project ContainerUserControl. By default, UserControl1.vb is created. In Solution Explorer, right-click UserControl1.vb, and then click View Code.  Add the following code to the Declarations section: Imports System.ComponentModel Imports System.ComponentModel.Design   Apply the System.ComponentModel.DesignerAttribute attribute to the control as follows: <Designer(&quot;System.Windows.Forms.Design.ParentControlDesigner,System.Design&quot;, GetType(IDesigner))> _ Public Class UserControl1 Inherits System.Windows.Forms.UserControl

...

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

back to the top

Test the UserControl

 * 1) On the File menu, point to Add Project, and then click New Project.
 * 2) Under Project Types, click Visual Basic Projects. Under Templates, click Windows Application. Click OK. By default, Form1.vb is created.
 * 3) Drag UserControl1 from the toolbox (under Windows Forms) to Form1.vb.
 * 4) Drag a Button from the toolbox to UserControl1.
 * 5) Note that the UserControl1 object behaves as control container for the button.

back to the top

<div class="references_section">