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 topOverview
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 topCreate 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("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _
Public Class UserControl1
Inherits System.Windows.Forms.UserControl
...
End Class
- On the Build menu, click Build
Solution.
back to the topTest the UserControl
- On the File menu, point to Add
Project, and then click New Project.
- Under Project Types, click Visual
Basic Projects. Under Templates, click
Windows Application. Click OK. By default,
Form1.vb is created.
- Drag UserControl1 from the toolbox (under
Windows Forms) to Form1.vb.
- Drag a Button from the toolbox to
UserControl1.
- Note that the UserControl1 object behaves as
control container for the button.
back to the top