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


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

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

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Create a new Windows Control Library project by using Visual Basic .NET or Visual Basic 2005.
  3. Name the project ContainerUserControl. By default, UserControl1.vb is created.
  4. In Solution Explorer, right-click
    UserControl1.vb, and then click View Code.
  5. Add the following code to the Declarations section:
    Imports System.ComponentModel
    Imports System.ComponentModel.Design
  6. 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
  7. On the Build menu, click Build Solution.
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
Propriedades

ID do Artigo: 322222 - Última Revisão: 16 de jan de 2008 - Revisão: 1

Comentários