Condividi tramite


How to: Create and Configure Components in Design Mode

Caution

This content was written for .NET Framework. If you're using .NET 6 or a later version, use this content with caution. The designer system has changed for Windows Forms and it's important that you review the Designer changes since .NET Framework article.

Your custom designer can programmatically create and configure components on the design surface.

Example

The following code example demonstrates how to use designer services to create and initialize components in your custom designer. A designer class called DemoControlDesigner is attached to a DemoControl class and performs the following operations:

  • Creates and configures a Timer component for display on the design surface.

  • Removes a Timer component from the design surface.

  • Initializes an instance of the DemoControl class when it is created by the Toolbox.

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Diagnostics
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Data
Imports System.Reflection
Imports System.Runtime.Serialization
Imports System.Text
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Windows.Forms.Design.Behavior

Public Class Form1
   Inherits Form
   Private demoControl1 As DemoControl

   Private components As System.ComponentModel.IContainer = Nothing 


   Public Sub New()
      InitializeComponent()
   End Sub 


   Protected Overrides Sub Dispose(disposing As Boolean)
      If disposing AndAlso (components IsNot Nothing) Then
         components.Dispose()
      End If 
      MyBase.Dispose(disposing)
   End Sub


   <STAThread()>  _
   Shared Sub Main()
      Application.EnableVisualStyles()
      Application.Run(New Form1())
   End Sub

   #Region "Windows Form Designer generated code" 


   Private Sub InitializeComponent()
        Me.demoControl1 = New DemoControl
        Me.SuspendLayout()
        ' 
        'demoControl1 
        ' 
        Me.demoControl1.Location = New System.Drawing.Point(53, 54)
        Me.demoControl1.Name = "demoControl1" 
        Me.demoControl1.TabIndex = 0
        ' 
        'Form1 
        ' 
        Me.ClientSize = New System.Drawing.Size(492, 482)
        Me.Controls.Add(Me.demoControl1)
        Me.Name = "Form1" 
        Me.Text = "r" 
        Me.ResumeLayout(False)

    End Sub

   #End Region

End Class 

' This control is derived from UserContro, with no additional logic. 
' The design-related code is implemented in DemoControlDesigner.

<DesignerAttribute(GetType(DemoControlDesigner)), _
ToolboxItem(GetType(DemoControl.DemoToolboxItem))> _
Public Class DemoControl
    Inherits Label

    Private components As System.ComponentModel.IContainer = Nothing 


    Public Sub New()
        InitializeComponent()

        MessageBox.Show("DemoControl", "Constructor")
    End Sub 


    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If 
        MyBase.Dispose(disposing)
    End Sub 


    Private Sub InitializeComponent()
    End Sub 


    ' Toolbox items must be serializable.
    <Serializable(), _
    System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Name:="FullTrust"), _
    System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name:="FullTrust")> _
    Class DemoToolboxItem
        Inherits ToolboxItem

        ' The add components dialog in VS looks for a public 
        ' ctor that takes a type. 
        Public Sub New(ByVal toolType As Type)
            MyBase.New(toolType)
        End Sub 'New 


        ' And you must provide this special constructor for serialization. 
        ' If you add additional data to MyToolboxItem that you 
        ' want to serialize, you may override Deserialize and 
        ' Serialize methods to add that data.   
        Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
            Deserialize(info, context)
        End Sub 'New 


        ' This implementation sets the new control's Text and  
        ' AutoSize properties. 
        Protected Overrides Function CreateComponentsCore(ByVal host As IDesignerHost, ByVal defaultValues As IDictionary) As IComponent()

            Dim comps As IComponent() = MyBase.CreateComponentsCore(host, defaultValues)

            ' The returned IComponent array contains a single  
            ' component, which is an instance of DemoControl. 
            CType(comps(0), DemoControl).Text = "This text was set by CreateComponentsCore." 
            CType(comps(0), DemoControl).AutoSize = True 

            Return comps
        End Function 
    End Class 

End Class 



' This class demonstrates a designer that attaches to various  
' services and changes the properties exposed by the control 
' being designed.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Class DemoControlDesigner
    Inherits ControlDesigner

    ' This member backs the Locked property. 
    Private lockedValue As Boolean = False 

    ' This is the collection of DesignerActionLists that 
    ' defines the smart tags offered on the control.  
    Private actionListsValue As DesignerActionListCollection = Nothing 

    ' This Timer is created when you select the Create Timer 
    ' smart tag item. 
    Private createdTimer As Timer = Nothing 

    ' These are the services which DemoControlDesigner will use. 
    Private actionService As DesignerActionService = Nothing 
    Private actionUiService As DesignerActionUIService = Nothing 
    Private changeService As IComponentChangeService = Nothing 
    Private eventService As IDesignerEventService = Nothing 
    Private host As IDesignerHost = Nothing 
    Private optionService As IDesignerOptionService = Nothing 
    Private eventBindingService As IEventBindingService = Nothing 
    Private listService As IExtenderListService = Nothing 
    Private referenceService As IReferenceService = Nothing 
    Private selectionService As ISelectionService = Nothing 
    Private typeResService As ITypeResolutionService = Nothing 
    Private componentDiscoveryService As IComponentDiscoveryService = Nothing 
    Private toolboxService As IToolboxService = Nothing 
    Private undoEng As UndoEngine = Nothing 

    Public Sub New()
        MessageBox.Show("DemoControlDesigner", "Constructor")
    End Sub 

    ' The Dispose method override is implemented so event handlers 
    ' can be removed. This prevents objects from lingering in  
    ' memory beyond the desired lifespan. 
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing Then 
            If (Me.changeService IsNot Nothing) Then 
                ' Unhook event handlers. 
                RemoveHandler Me.changeService.ComponentChanged, AddressOf ChangeService_ComponentChanged

                RemoveHandler Me.changeService.ComponentAdded, AddressOf ChangeService_ComponentAdded

                RemoveHandler Me.changeService.ComponentRemoved, AddressOf changeService_ComponentRemoved
            End If 

            If (Me.eventService IsNot Nothing) Then 
                RemoveHandler Me.eventService.ActiveDesignerChanged, AddressOf eventService_ActiveDesignerChanged
            End If 

            If (Me.selectionService IsNot Nothing) Then 
                RemoveHandler Me.selectionService.SelectionChanged, AddressOf selectionService_SelectionChanged
            End If 
        End If 


        MyBase.Dispose(disposing)

    End Sub 

    ' This method initializes the designer. 
    Public Overrides Sub Initialize(ByVal component As IComponent)

        MyBase.Initialize(component)

        ' Connect to various designer services.
        InitializeServices()

        ' Set up the BackColor value that will be serialized. 
        ' This is the shadowed property on the designer. 
        Me.BackColor = Color.Chartreuse

        ' Set up the BackColor value that will be displayed. 
        Me.Control.BackColor = Color.AliceBlue

    End Sub 'Initialize 

    ' This method creates the DesignerActionList on demand, causing 
    ' smart tags to appear on the control being designed. 

    Public Overrides ReadOnly Property ActionLists() As DesignerActionListCollection
        Get 
            If actionListsValue Is Nothing Then
                actionListsValue = New DesignerActionListCollection()
                actionListsValue.Add(New DemoActionList(Me.Component))
            End If 

            Return actionListsValue
        End Get 
    End Property 

    ' This utility method connects the designer to various 
    ' services it will use.  
    Private Sub InitializeServices()

        ' Acquire a reference to DesignerActionService. 
        Me.actionService = GetService(GetType(DesignerActionService))

        ' Acquire a reference to DesignerActionUIService. 
        Me.actionUiService = GetService(GetType(DesignerActionUIService))

        ' Acquire a reference to IComponentChangeService. 
        Me.changeService = GetService(GetType(IComponentChangeService))

        ' Hook the IComponentChangeService events. 
        If (Me.changeService IsNot Nothing) Then 
            AddHandler Me.changeService.ComponentChanged, AddressOf ChangeService_ComponentChanged

            AddHandler Me.changeService.ComponentAdded, AddressOf ChangeService_ComponentAdded

            AddHandler Me.changeService.ComponentRemoved, AddressOf changeService_ComponentRemoved
        End If 

        ' Acquire a reference to ISelectionService. 
        Me.selectionService = GetService(GetType(ISelectionService))

        ' Hook the SelectionChanged event. 
        If (Me.selectionService IsNot Nothing) Then 
            AddHandler Me.selectionService.SelectionChanged, AddressOf selectionService_SelectionChanged
        End If 

        ' Acquire a reference to IDesignerEventService. 
        Me.eventService = GetService(GetType(IDesignerEventService))

        If (Me.eventService IsNot Nothing) Then 
            AddHandler Me.eventService.ActiveDesignerChanged, AddressOf eventService_ActiveDesignerChanged
        End If 

        ' Acquire a reference to IDesignerHost. 
        Me.host = GetService(GetType(IDesignerHost))

        ' Acquire a reference to IDesignerOptionService. 
        Me.optionService = GetService(GetType(IDesignerOptionService))

        ' Acquire a reference to IEventBindingService. 
        Me.eventBindingService = GetService(GetType(IEventBindingService))

        ' Acquire a reference to IExtenderListService. 
        Me.listService = GetService(GetType(IExtenderListService))

        ' Acquire a reference to IReferenceService. 
        Me.referenceService = GetService(GetType(IReferenceService))

        ' Acquire a reference to ITypeResolutionService. 
        Me.typeResService = GetService(GetType(ITypeResolutionService))

        ' Acquire a reference to IComponentDiscoveryService. 
        Me.componentDiscoveryService = GetService(GetType(IComponentDiscoveryService))

        ' Acquire a reference to IToolboxService. 
        Me.toolboxService = GetService(GetType(IToolboxService))

        ' Acquire a reference to UndoEngine. 
        Me.undoEng = GetService(GetType(UndoEngine))

        If (Me.undoEng IsNot Nothing) Then
            MessageBox.Show("UndoEngine")
        End If 
    End Sub 

    ' This is the shadowed property on the designer. 
    ' This value will be serialized instead of the  
    ' value of the control's property. 

    Public Property BackColor() As Color
        Get 
            Return CType(ShadowProperties("BackColor"), Color)
        End Get 
        Set(ByVal value As Color)
            If (Me.changeService IsNot Nothing) Then 
                Dim backColorDesc As PropertyDescriptor = TypeDescriptor.GetProperties(Me.Control)("BackColor")

                Me.changeService.OnComponentChanging(Me.Control, backColorDesc)

                Me.ShadowProperties("BackColor") = value

                Me.changeService.OnComponentChanged(Me.Control, backColorDesc, Nothing, Nothing)
            End If 
        End Set 
    End Property 

    ' This is the property added by the designer in the 
    ' PreFilterProperties method. 

    Private Property Locked() As Boolean 
        Get 
            Return lockedValue
        End Get 
        Set(ByVal value As Boolean)
            lockedValue = value
        End Set 
    End Property 

    ' The PreFilterProperties method is where you can add or remove 
    ' properties from the component being designed.  
    ' 
    ' In this implementation, the Visible property is removed, 
    ' the BackColor property is shadowed by the designer, and 
    ' the a new property, called Locked, is added. 
    Protected Overrides Sub PreFilterProperties(ByVal properties As IDictionary)

        ' Always call the base PreFilterProperties implementation  
        ' before you modify the properties collection. 
        MyBase.PreFilterProperties(properties)

        ' Remove the visible property.
        properties.Remove("Visible")

        ' Shadow the BackColor property. 
        Dim propertyDesc As PropertyDescriptor = TypeDescriptor.CreateProperty(GetType(DemoControlDesigner), CType(properties("BackColor"), PropertyDescriptor), New Attribute(-1) {})
        properties("BackColor") = propertyDesc

        ' Create the Locked property.
        properties("Locked") = TypeDescriptor.CreateProperty(GetType(DemoControlDesigner), "Locked", GetType(Boolean), CategoryAttribute.Design, DesignOnlyAttribute.Yes)

    End Sub 

    ' The PostFilterProperties method is where you modify existing 
    ' properties. You must only use this method to modify existing  
    ' items. Do not add or remove items here. Also, be sure to  
    ' call base.PostFilterProperties(properties) after your filtering 
    ' logic. 
    ' 
    ' In this implementation, the Enabled property is hidden from 
    ' any PropertyGrid or Properties window. This is done by  
    ' creating a copy of the existing PropertyDescriptor and 
    ' attaching two new Attributes: Browsable and EditorBrowsable. 
    Protected Overrides Sub PostFilterProperties(ByVal properties As IDictionary)
        Dim pd As PropertyDescriptor = properties("Enabled")

        pd = TypeDescriptor.CreateProperty(pd.ComponentType, pd, New Attribute(1) {New BrowsableAttribute(False), New EditorBrowsableAttribute(EditorBrowsableState.Never)})

        properties(pd.Name) = pd

        ' Always call the base PostFilterProperties implementation  
        ' after you modify the properties collection. 
        MyBase.PostFilterProperties(properties)

    End Sub

#Region "Event Handlers" 

    Private Sub eventService_ActiveDesignerChanged(ByVal sender As Object, ByVal e As ActiveDesignerEventArgs)
        If (e.NewDesigner IsNot Nothing) Then 
            Dim o As Object = e.NewDesigner
            MessageBox.Show(o.ToString(), "ActiveDesignerChanged")
        End If 
    End Sub 

    Private Sub ChangeService_ComponentChanged(ByVal sender As Object, ByVal e As ComponentChangedEventArgs)
        Dim msg As String = String.Format("{0}, {1}", e.Component, e.Member)

        MessageBox.Show(msg, "ComponentChanged")
    End Sub 


    Private Sub ChangeService_ComponentAdded(ByVal sender As Object, ByVal e As ComponentEventArgs)
        MessageBox.Show(e.ToString(), "ComponentAdded")
    End Sub 


    Private Sub changeService_ComponentRemoved(ByVal sender As Object, ByVal e As ComponentEventArgs)
        Dim o As Object = e.Component
        MessageBox.Show(o.ToString(), "ComponentRemoved")
    End Sub 

    Private Sub selectionService_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
        If (Me.selectionService IsNot Nothing) Then 
            If Me.selectionService.PrimarySelection Is Me.Control Then
                MessageBox.Show(Me.Control.ToString(), "SelectionChanged")
            End If 
        End If 
    End Sub

#End Region

    ' This class defines the smart tags that appear on the control 
    ' that is being designed. 

    Friend Class DemoActionList
        Inherits System.ComponentModel.Design.DesignerActionList
        ' Cache a reference to the designer host. 
        Private host As IDesignerHost = Nothing 

        ' Cache a reference to the control. 
        Private relatedControl As DemoControl = Nothing 

        ' Cache a reference to the designer. 
        Private relatedDesigner As DemoControlDesigner = Nothing 

        'The constructor associates the control  
        'with the smart tag list. 
        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
            Me.relatedControl = component '

            Me.host = Me.Component.Site.GetService(GetType(IDesignerHost))

            Dim dcd As IDesigner = host.GetDesigner(Me.Component)
            Me.relatedDesigner = dcd

        End Sub 

        ' This method creates and populates the  
        ' DesignerActionItemCollection which is used to  
        ' display smart tag items. 
        Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
            Dim items As New DesignerActionItemCollection()

            ' If the Timer component has not been created, show the 
            ' "Create Timer" DesignerAction item. 
            ' 
            ' If the Timer component exists, show the timer-related 
            ' options. 
            If Me.relatedDesigner.createdTimer Is Nothing Then
                items.Add(New DesignerActionMethodItem(Me, "CreateTimer", "Create Timer", True))
            Else
                items.Add(New DesignerActionMethodItem(Me, "ShowEventHandlerCode", "Show Event Handler Code", True))

                items.Add(New DesignerActionMethodItem(Me, "RemoveTimer", "Remove Timer", True))
            End If

            items.Add(New DesignerActionMethodItem(Me, "GetExtenderProviders", "Get Extender Providers", True))

            items.Add(New DesignerActionMethodItem(Me, "GetDemoControlReferences", "Get DemoControl References", True))

            items.Add(New DesignerActionMethodItem(Me, "GetPathOfAssembly", "Get Path of Executing Assembly", True))

            items.Add(New DesignerActionMethodItem(Me, "GetComponentTypes", "Get ScrollableControl Types", True))

            items.Add(New DesignerActionMethodItem(Me, "GetToolboxCategories", "Get Toolbox Categories", True))

            items.Add(New DesignerActionMethodItem(Me, "SetBackColor", "Set Back Color", True))

            Return items
        End Function 

        ' This method creates a Timer component using the  
        ' IDesignerHost.CreateComponent method. It also  
        ' creates an event handler for the Timer component's 
        ' tick event. 
        Private Sub CreateTimer()
            If (Me.host IsNot Nothing) Then 
                If Me.relatedDesigner.createdTimer Is Nothing Then 
                    ' Create and configure the Timer object. 
                    Me.relatedDesigner.createdTimer = Me.host.CreateComponent(GetType(Timer))

                    Dim t As Timer = Me.relatedDesigner.createdTimer
                    t.Interval = 1000
                    t.Enabled = True 

                    Dim eventColl As EventDescriptorCollection = TypeDescriptor.GetEvents(t, New Attribute(-1) {})

                    If (eventColl IsNot Nothing) Then 
                        Dim ed As EventDescriptor = eventColl("Tick")

                        If (ed IsNot Nothing) Then 
                            Dim epd As PropertyDescriptor = Me.relatedDesigner.eventBindingService.GetEventProperty(ed)

                            epd.SetValue(t, "timer_Tick")
                        End If 
                    End If 

                    Me.relatedDesigner.actionUiService.Refresh(Me.relatedControl)
                End If 
            End If 
        End Sub 

        ' This method uses the IEventBindingService.ShowCode 
        ' method to start the Code Editor. It places the caret 
        ' in the timer_tick method created by the CreateTimer method. 
        Private Sub ShowEventHandlerCode()
            Dim t As Timer = Me.relatedDesigner.createdTimer

            If (t IsNot Nothing) Then 
                Dim eventColl As EventDescriptorCollection = TypeDescriptor.GetEvents(t, New Attribute(-1) {})
                If (eventColl IsNot Nothing) Then 
                    Dim ed As EventDescriptor = eventColl("Tick")

                    If (ed IsNot Nothing) Then 
                        Me.relatedDesigner.eventBindingService.ShowCode(t, ed)
                    End If 
                End If 
            End If 
        End Sub 

        ' This method uses the IDesignerHost.DestroyComponent method 
        ' to remove the Timer component from the design environment. 
        Private Sub RemoveTimer()
            If (Me.host IsNot Nothing) Then 
                If (Me.relatedDesigner.createdTimer IsNot Nothing) Then 
                    Me.host.DestroyComponent(Me.relatedDesigner.createdTimer)

                    Me.relatedDesigner.createdTimer = Nothing 

                    Me.relatedDesigner.actionUiService.Refresh(Me.relatedControl)
                End If 
            End If 
        End Sub 

        ' This method uses IExtenderListService.GetExtenderProviders 
        ' to enumerate all the extender providers and display them  
        ' in a MessageBox. 
        Private Sub GetExtenderProviders()
            If (Me.relatedDesigner.listService IsNot Nothing) Then 
                Dim sb As New StringBuilder()

                Dim providers As IExtenderProvider() = Me.relatedDesigner.listService.GetExtenderProviders()

                Dim i As Integer 
                For i = 0 To providers.Length - 1
                    Dim o As Object = providers(i)
                    sb.Append(o.ToString())
                    sb.Append(ControlChars.Cr + ControlChars.Lf)
                Next i

                MessageBox.Show(sb.ToString(), "Extender Providers")
            End If 
        End Sub 

        ' This method uses the IReferenceService.GetReferences method 
        ' to enumerate all the instances of DemoControl on the  
        ' design surface. 
        Private Sub GetDemoControlReferences()
            If (Me.relatedDesigner.referenceService IsNot Nothing) Then 
                Dim sb As New StringBuilder()

                Dim refs As Object() = Me.relatedDesigner.referenceService.GetReferences(GetType(DemoControl))

                Dim i As Integer 
                For i = 0 To refs.Length - 1
                    sb.Append(refs(i).ToString())
                    sb.Append(ControlChars.Cr + ControlChars.Lf)
                Next i

                MessageBox.Show(sb.ToString(), "DemoControl References")
            End If 
        End Sub 

        ' This method uses the ITypeResolutionService.GetPathOfAssembly 
        ' method to display the path of the executing assembly. 
        Private Sub GetPathOfAssembly()
            If (Me.relatedDesigner.typeResService IsNot Nothing) Then 
                Dim name As System.Reflection.AssemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName()

                MessageBox.Show(Me.relatedDesigner.typeResService.GetPathOfAssembly(name), "Path of executing assembly")
            End If 
        End Sub 


        ' This method uses the IComponentDiscoveryService.GetComponentTypes  
        ' method to find all the types that derive from  
        ' ScrollableControl. 
        Private Sub GetComponentTypes()
            If (Me.relatedDesigner.componentDiscoveryService IsNot Nothing) Then 
                Dim components As ICollection = Me.relatedDesigner.componentDiscoveryService.GetComponentTypes(host, GetType(ScrollableControl))

                If (components IsNot Nothing) Then 
                    If components.Count > 0 Then 
                        Dim sb As New StringBuilder()

                        Dim e As IEnumerator = components.GetEnumerator()

                        While e.MoveNext()
                            sb.Append(e.Current.ToString())
                            sb.Append(ControlChars.Cr + ControlChars.Lf)
                        End While

                        MessageBox.Show(sb.ToString(), "Controls derived from ScrollableControl")
                    End If 
                End If 
            End If 
        End Sub 


        ' This method uses the IToolboxService.CategoryNames 
        ' method to enumerate all the categories that appear 
        ' in the Toolbox. 
        Private Sub GetToolboxCategories()
            If (Me.relatedDesigner.toolboxService IsNot Nothing) Then 
                Dim sb As New StringBuilder()

                Dim names As CategoryNameCollection = Me.relatedDesigner.toolboxService.CategoryNames

                Dim name As String 
                For Each name In names
                    sb.Append(name.ToString())
                    sb.Append(ControlChars.Cr + ControlChars.Lf)
                Next name

                MessageBox.Show(sb.ToString(), "Toolbox Categories")
            End If 
        End Sub 


        ' This method sets the shadowed BackColor property on the  
        ' designer. This is the value that is serialized by the  
        ' design environment. 
        Private Sub SetBackColor()
            Dim d As New ColorDialog()
            If d.ShowDialog() = DialogResult.OK Then 
                Me.relatedDesigner.BackColor = d.Color
            End If 
        End Sub 
    End Class 
End Class
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Data;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Forms.Design.Behavior;

public class Form1 : Form
{
    private DemoControl demoControl1;

    private System.ComponentModel.IContainer components = null;

    public Form1()
    {
        InitializeComponent();
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

    #region Windows Form Designer generated code

    private void InitializeComponent()
    {
        this.demoControl1 = new DemoControl();
        this.SuspendLayout();
        //  
        // demoControl1 
        //  
        this.demoControl1.AutoSize = true;
        this.demoControl1.BackColor = System.Drawing.Color.Chartreuse;
        this.demoControl1.Location = new System.Drawing.Point(0, 0);
        this.demoControl1.Name = "demoControl1";
        this.demoControl1.Size = new System.Drawing.Size(232, 14);
        this.demoControl1.TabIndex = 0;
        this.demoControl1.Text = "This text was set by CreateComponentsCore.";
        //  
        // Form1 
        //  
        this.ClientSize = new System.Drawing.Size(492, 482);
        this.Controls.Add(this.demoControl1);
        this.Name = "Form1";
        this.Text = "r";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

}

// This control is derived from UserControl, with only a little 
// added logic for the Toolbox interaction. 
// 
// All of the custom designer code is implemented in the 
// DemoControlDesigner class.

[DesignerAttribute(typeof(DemoControlDesigner))]
[ToolboxItem(typeof(DemoToolboxItem))]
public class DemoControl : Label
{

    private System.ComponentModel.IContainer components = null;

    public DemoControl()
    {
        InitializeComponent();

        MessageBox.Show("DemoControl", "Constructor");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        //  
        // DemoControl 
        //  
        this.Name = "DemoControl";
    }

    // Toolbox items must be serializable.
    [Serializable]
    [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Name="FullTrust")]
    [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name="FullTrust")]
    class DemoToolboxItem : ToolboxItem
    {
        // The add components dialog in VS looks for a public 
        // ctor that takes a type. 
        public DemoToolboxItem(Type toolType)
            : base(toolType)
        {
        }

        // And you must provide this special constructor for serialization. 
        // If you add additional data to MyToolboxItem that you 
        // want to serialize, you may override Deserialize and 
        // Serialize methods to add that data.  
        DemoToolboxItem(SerializationInfo info, StreamingContext context)
        {
            Deserialize(info, context);
        }

        // This implementation sets the new control's Text and  
        // AutoSize properties. 
        protected override IComponent[] CreateComponentsCore(
            IDesignerHost host, 
            IDictionary defaultValues)
        {

            IComponent[] comps = base.CreateComponentsCore(host, defaultValues);

            // The returned IComponent array contains a single  
            // component, which is an instance of DemoControl.
            ((DemoControl)comps[0]).Text = "This text was set by CreateComponentsCore.";
            ((DemoControl)comps[0]).AutoSize = true;

            return comps;
        }
    }
}

// This class demonstrates a designer that attaches to various  
// services and changes the properties exposed by the control 
// being designed.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
public class DemoControlDesigner : ControlDesigner
{
    // This member backs the Locked property. 
    private bool lockedValue = false;

    // This is the collection of DesignerActionLists that 
    // defines the smart tags offered on the control.  
    private DesignerActionListCollection actionLists = null;

    // This Timer is created when you select the Create Timer 
    // smart tag item. 
    private Timer createdTimer = null;

    // These are the services which DemoControlDesigner will use. 
    private DesignerActionService actionService = null;
    private DesignerActionUIService actionUiService = null;
    private IComponentChangeService changeService = null;
    private IDesignerEventService eventService = null;
    private IDesignerHost host = null;
    private IDesignerOptionService optionService = null;
    private IEventBindingService eventBindingService = null;
    private IExtenderListService listService = null;
    private IReferenceService referenceService = null;
    private ISelectionService selectionService = null;
    private ITypeResolutionService typeResService = null;
    private IComponentDiscoveryService componentDiscoveryService = null;
    private IToolboxService toolboxService = null;
    private UndoEngine undoEng = null;

    public DemoControlDesigner()
    {
        MessageBox.Show("DemoControlDesigner", "Constructor");
    }

    // The Dispose method override is implemented so event handlers 
    // can be removed. This prevents objects from lingering in  
    // memory beyond the desired lifespan. 
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (this.changeService != null)
            {
                // Unhook event handlers. 
                this.changeService.ComponentChanged -=
                    new ComponentChangedEventHandler(
                    ChangeService_ComponentChanged);

                this.changeService.ComponentAdded -=
                    new ComponentEventHandler(
                    ChangeService_ComponentAdded);

                this.changeService.ComponentRemoved -=
                    new ComponentEventHandler(
                    changeService_ComponentRemoved);
            }

            if (this.eventService != null)
            {
                this.eventService.ActiveDesignerChanged -=
                    new ActiveDesignerEventHandler(
                    eventService_ActiveDesignerChanged);
            }

            if (this.selectionService != null)
            {
                this.selectionService.SelectionChanged -=
                    new EventHandler(
                    selectionService_SelectionChanged);
            }

        }

        base.Dispose(disposing);
    }

    // This method initializes the designer. 
    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        // Connect to various designer services.
        InitializeServices();

        // Set up the BackColor value that will be serialized. 
        // This is the shadowed property on the designer. 
        this.BackColor = Color.Chartreuse;

        // Set up the BackColor value that will be displayed. 
        this.Control.BackColor = Color.AliceBlue;

    }

    // This method creates the DesignerActionList on demand, causing 
    // smart tags to appear on the control being designed. 
    public override DesignerActionListCollection ActionLists
    {
        get
        {
            if (null == actionLists)
            {
                actionLists = new DesignerActionListCollection();
                actionLists.Add(
                    new DemoActionList(this.Component));
            }

            return actionLists;
        }
    }

    // This utility method connects the designer to various 
    // services it will use.  
    private void InitializeServices()
    {
        // Acquire a reference to DesignerActionService. 
        this.actionService =
            GetService(typeof(DesignerActionService))
            as DesignerActionService;

        // Acquire a reference to DesignerActionUIService. 
        this.actionUiService =
            GetService(typeof(DesignerActionUIService))
            as DesignerActionUIService;

        // Acquire a reference to IComponentChangeService. 
        this.changeService =
            GetService(typeof(IComponentChangeService))
            as IComponentChangeService;

        // Hook the IComponentChangeService events. 
        if (this.changeService != null)
        {
            this.changeService.ComponentChanged +=
                new ComponentChangedEventHandler(
                ChangeService_ComponentChanged);

            this.changeService.ComponentAdded +=
                new ComponentEventHandler(
                ChangeService_ComponentAdded);

            this.changeService.ComponentRemoved +=
                new ComponentEventHandler(
                changeService_ComponentRemoved);
        }

        // Acquire a reference to ISelectionService. 
        this.selectionService =
            GetService(typeof(ISelectionService))
            as ISelectionService;

        // Hook the SelectionChanged event. 
        if (this.selectionService != null)
        {
            this.selectionService.SelectionChanged +=
                new EventHandler(selectionService_SelectionChanged);
        }

        // Acquire a reference to IDesignerEventService. 
        this.eventService =
            GetService(typeof(IDesignerEventService))
            as IDesignerEventService;

        if (this.eventService != null)
        {
            this.eventService.ActiveDesignerChanged +=
                new ActiveDesignerEventHandler(
                eventService_ActiveDesignerChanged);
        }

        // Acquire a reference to IDesignerHost. 
        this.host =
            GetService(typeof(IDesignerHost))
            as IDesignerHost;

        // Acquire a reference to IDesignerOptionService. 
        this.optionService =
            GetService(typeof(IDesignerOptionService))
            as IDesignerOptionService;

        // Acquire a reference to IEventBindingService. 
        this.eventBindingService =
            GetService(typeof(IEventBindingService))
            as IEventBindingService;

        // Acquire a reference to IExtenderListService. 
        this.listService =
            GetService(typeof(IExtenderListService))
            as IExtenderListService;

        // Acquire a reference to IReferenceService. 
        this.referenceService =
            GetService(typeof(IReferenceService))
            as IReferenceService;

        // Acquire a reference to ITypeResolutionService. 
        this.typeResService =
            GetService(typeof(ITypeResolutionService))
            as ITypeResolutionService;

        // Acquire a reference to IComponentDiscoveryService. 
        this.componentDiscoveryService =
            GetService(typeof(IComponentDiscoveryService))
            as IComponentDiscoveryService;

        // Acquire a reference to IToolboxService. 
        this.toolboxService =
            GetService(typeof(IToolboxService))
            as IToolboxService;

        // Acquire a reference to UndoEngine. 
        this.undoEng =
            GetService(typeof(UndoEngine))
            as UndoEngine;

        if (this.undoEng != null)
        {
            MessageBox.Show("UndoEngine");
        }
    }

    // This is the shadowed property on the designer. 
    // This value will be serialized instead of the  
    // value of the control's property. 
    public Color BackColor
    {
        get
        {
            return (Color)ShadowProperties["BackColor"];
        }
        set
        {
            if (this.changeService != null)
            {
                PropertyDescriptor backColorDesc =
                    TypeDescriptor.GetProperties(this.Control)["BackColor"];

                this.changeService.OnComponentChanging(
                    this.Control,
                    backColorDesc);

                this.ShadowProperties["BackColor"] = value;

                this.changeService.OnComponentChanged(
                    this.Control,
                    backColorDesc,
                    null,
                    null);
            }
        }
    }

    // This is the property added by the designer in the 
    // PreFilterProperties method. 
    private bool Locked
    {
        get
        {
            return lockedValue;
        }
        set
        {
            lockedValue = value;
        }
    }

    // The PreFilterProperties method is where you can add or remove 
    // properties from the component being designed.  
    // 
    // In this implementation, the Visible property is removed, 
    // the BackColor property is shadowed by the designer, and 
    // the a new property, called Locked, is added. 
    protected override void PreFilterProperties(IDictionary properties)
    {
        // Always call the base PreFilterProperties implementation  
        // before you modify the properties collection. 
        base.PreFilterProperties(properties);

        // Remove the visible property.
        properties.Remove("Visible");

        // Shadow the BackColor property.
        PropertyDescriptor propertyDesc = TypeDescriptor.CreateProperty(
            typeof(DemoControlDesigner),
            (PropertyDescriptor)properties["BackColor"],
            new Attribute[0]);
        properties["BackColor"] = propertyDesc;

        // Create the Locked property.
        properties["Locked"] = TypeDescriptor.CreateProperty(
                 typeof(DemoControlDesigner),
                 "Locked",
                 typeof(bool),
                 CategoryAttribute.Design,
                 DesignOnlyAttribute.Yes);
    }

    // The PostFilterProperties method is where you modify existing 
    // properties. You must only use this method to modify existing  
    // items. Do not add or remove items here. Also, be sure to  
    // call base.PostFilterProperties(properties) after your filtering 
    // logic. 
    // 
    // In this implementation, the Enabled property is hidden from 
    // any PropertyGrid or Properties window. This is done by  
    // creating a copy of the existing PropertyDescriptor and 
    // attaching two new Attributes: Browsable and EditorBrowsable. 

    protected override void PostFilterProperties(IDictionary properties)
    {
        PropertyDescriptor pd =
            properties["Enabled"] as PropertyDescriptor;

        pd = TypeDescriptor.CreateProperty(
            pd.ComponentType,
            pd,
            new Attribute[2] { 
                new BrowsableAttribute(false),
                new EditorBrowsableAttribute(EditorBrowsableState.Never)});

        properties[pd.Name] = pd;

        // Always call the base PostFilterProperties implementation  
        // after you modify the properties collection. 
        base.PostFilterProperties(properties);
    }

    #region Event Handlers

    void eventService_ActiveDesignerChanged(
        object sender,
        ActiveDesignerEventArgs e)
    {
        if (e.NewDesigner != null)
        {
            MessageBox.Show(
                e.NewDesigner.ToString(),
                "ActiveDesignerChanged");
        }
    }


    void ChangeService_ComponentChanged(
        object sender,
        ComponentChangedEventArgs e)
    {
        string msg = String.Format(
            "{0}, {1}", e.Component, e.Member);

        MessageBox.Show(msg, "ComponentChanged");
    }

    void ChangeService_ComponentAdded(
        object sender,
        ComponentEventArgs e)
    {
        MessageBox.Show(
            e.Component.ToString(),
            "ComponentAdded");
    }

    void changeService_ComponentRemoved(
        object sender,
        ComponentEventArgs e)
    {
        MessageBox.Show(
            e.Component.ToString(),
            "ComponentRemoved");
    }

    void selectionService_SelectionChanged(
        object sender,
        EventArgs e)
    {
        if (this.selectionService != null)
        {
            if (this.selectionService.PrimarySelection == this.Control)
            {
                MessageBox.Show(
                    this.Control.ToString(),
                    "SelectionChanged");
            }
        }
    }

    #endregion

    // This class defines the smart tags that appear on the control 
    // that is being designed. 
    internal class DemoActionList :
          System.ComponentModel.Design.DesignerActionList
    {
        // Cache a reference to the designer host. 
        private IDesignerHost host = null;

        // Cache a reference to the control. 
        private DemoControl relatedControl = null;

        // Cache a reference to the designer. 
        private DemoControlDesigner relatedDesigner = null;

        //The constructor associates the control  
        //with the smart tag list. 
        public DemoActionList(IComponent component)
            : base(component)
        {
            this.relatedControl = component as DemoControl;

            this.host =
                this.Component.Site.GetService(typeof(IDesignerHost))
                as IDesignerHost;

            IDesigner dcd = host.GetDesigner(this.Component);
            this.relatedDesigner = dcd as DemoControlDesigner;
        }

        // This method creates and populates the  
        // DesignerActionItemCollection which is used to  
        // display smart tag items. 
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items =
                new DesignerActionItemCollection();

            // If the Timer component has not been created, show the 
            // "Create Timer" DesignerAction item.
            // 
            // If the Timer component exists, show the timer-related 
            // options. 

            if (this.relatedDesigner.createdTimer == null)
            {
                items.Add(new DesignerActionMethodItem(
                    this,
                    "CreateTimer",
                    "Create Timer",
                    true));
            }
            else
            {   
                items.Add(new DesignerActionMethodItem(
                    this,
                    "ShowEventHandlerCode",
                    "Show Event Handler Code",
                    true));

                items.Add(new DesignerActionMethodItem(
                    this,
                    "RemoveTimer",
                    "Remove Timer",
                    true));
            }

            items.Add(new DesignerActionMethodItem(
               this,
               "GetExtenderProviders",
               "Get Extender Providers",
               true));

            items.Add(new DesignerActionMethodItem(
              this,
              "GetDemoControlReferences",
              "Get DemoControl References",
              true));

            items.Add(new DesignerActionMethodItem(
              this,
              "GetPathOfAssembly",
              "Get Path of Executing Assembly",
              true));

            items.Add(new DesignerActionMethodItem(
              this,
              "GetComponentTypes",
              "Get ScrollableControl Types",
              true));

            items.Add(new DesignerActionMethodItem(
                this,
                "GetToolboxCategories",
                "Get Toolbox Categories",
                true));

            items.Add(new DesignerActionMethodItem(
                this,
                "SetBackColor",
                "Set Back Color",
                true));

            return items;
        }

        // This method creates a Timer component using the  
        // IDesignerHost.CreateComponent method. It also  
        // creates an event handler for the Timer component's 
        // tick event. 
        private void CreateTimer()
        {
            if (this.host != null)
            {
                if (this.relatedDesigner.createdTimer == null)
                {
                    // Create and configure the Timer object. 
                    this.relatedDesigner.createdTimer =
                        this.host.CreateComponent(typeof(Timer)) as Timer;
                    Timer t = this.relatedDesigner.createdTimer;
                    t.Interval = 1000;
                    t.Enabled = true;

                    EventDescriptorCollection eventColl =
                        TypeDescriptor.GetEvents(t, new Attribute[0]);

                    if (eventColl != null)
                    {
                        EventDescriptor ed =
                            eventColl["Tick"] as EventDescriptor;
                        if (ed != null)
                        {
                            PropertyDescriptor epd =
                                this.relatedDesigner.eventBindingService.GetEventProperty(ed);

                            epd.SetValue(t, "timer_Tick");
                        }
                    }

                    this.relatedDesigner.actionUiService.Refresh(this.relatedControl);
                }
            }
        }

        // This method uses the IEventBindingService.ShowCode 
        // method to start the Code Editor. It places the caret 
        // in the timer_tick method created by the CreateTimer method. 
        private void ShowEventHandlerCode()
        {
            Timer t = this.relatedDesigner.createdTimer;

            if (t != null)
            {
                EventDescriptorCollection eventColl =
                    TypeDescriptor.GetEvents(t, new Attribute[0]);
                if (eventColl != null)
                {
                    EventDescriptor ed =
                        eventColl["Tick"] as EventDescriptor;
                    if (ed != null)
                    {
                        this.relatedDesigner.eventBindingService.ShowCode(t, ed);
                    }
                }
            }
        }

        // This method uses the IDesignerHost.DestroyComponent method 
        // to remove the Timer component from the design environment. 
        private void RemoveTimer()
        {
            if (this.host != null)
            {
                if (this.relatedDesigner.createdTimer != null)
                {
                    this.host.DestroyComponent(
                        this.relatedDesigner.createdTimer);

                    this.relatedDesigner.createdTimer = null;

                    this.relatedDesigner.actionUiService.Refresh(
                        this.relatedControl);
                }
            }
        }

        // This method uses IExtenderListService.GetExtenderProviders 
        // to enumerate all the extender providers and display them  
        // in a MessageBox. 
        private void GetExtenderProviders()
        {
            if (this.relatedDesigner.listService != null)
            {
                StringBuilder sb = new StringBuilder();

                IExtenderProvider[] providers =
                    this.relatedDesigner.listService.GetExtenderProviders();

                for (int i = 0; i < providers.Length; i++)
                {
                    sb.Append(providers[i].ToString());
                    sb.Append("\r\n");
                }

                MessageBox.Show(
                    sb.ToString(), 
                    "Extender Providers");
            }
        }

        // This method uses the IReferenceService.GetReferences method 
        // to enumerate all the instances of DemoControl on the  
        // design surface. 
        private void GetDemoControlReferences()
        {
            if (this.relatedDesigner.referenceService != null)
            {
                StringBuilder sb = new StringBuilder();

                object[] refs = this.relatedDesigner.referenceService.GetReferences(typeof(DemoControl));

                for (int i = 0; i < refs.Length; i++)
                {
                    sb.Append(refs[i].ToString());
                    sb.Append("\r\n");
                }

                MessageBox.Show(
                    sb.ToString(), 
                    "DemoControl References");
            }
        }


        // This method uses the ITypeResolutionService.GetPathOfAssembly 
        // method to display the path of the executing assembly. 
        private void GetPathOfAssembly()
        {
            if (this.relatedDesigner.typeResService != null)
            {
                System.Reflection.AssemblyName name =
                    System.Reflection.Assembly.GetExecutingAssembly().GetName();

                MessageBox.Show(
                    this.relatedDesigner.typeResService.GetPathOfAssembly(name),
                    "Path of executing assembly");
            }
        }

        // This method uses the IComponentDiscoveryService.GetComponentTypes  
        // method to find all the types that derive from  
        // ScrollableControl. 
        private void GetComponentTypes()
        {
            if (this.relatedDesigner.componentDiscoveryService != null)
            {
                ICollection components = this.relatedDesigner.componentDiscoveryService.GetComponentTypes(host, typeof(ScrollableControl));

                if (components != null)
                {
                    if (components.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();

                        IEnumerator e = components.GetEnumerator();

                        while (e.MoveNext())
                        {
                            sb.Append(e.Current.ToString());
                            sb.Append("\r\n");

                        }

                        MessageBox.Show(
                            sb.ToString(), 
                            "Controls derived from ScrollableControl");
                    }
                }
            }
        }

        // This method uses the IToolboxService.CategoryNames 
        // method to enumerate all the categories that appear 
        // in the Toolbox. 
        private void GetToolboxCategories()
        {
            if (this.relatedDesigner.toolboxService != null)
            {
                StringBuilder sb = new StringBuilder();

                CategoryNameCollection names = this.relatedDesigner.toolboxService.CategoryNames;

                foreach (string name in names)
                {
                    sb.Append(name.ToString());
                    sb.Append("\r\n");
                }

                MessageBox.Show(sb.ToString(), "Toolbox Categories");
            }
        }

        // This method sets the shadowed BackColor property on the  
        // designer. This is the value that is serialized by the  
        // design environment. 
        private void SetBackColor()
        {
            ColorDialog d = new ColorDialog();
            if (d.ShowDialog() == DialogResult.OK)
            {
                this.relatedDesigner.BackColor = d.Color;
            }
        }
    }
}

You can create and configure a component by requesting an instance from the design environment and setting its properties. You can also configure a component when it is created from the Toolbox.

Creating and Configuring a Component Programmatically

The following code example calls the CreateComponent method to create a Timer component. The Timer component is initialized by setting its Interval and Enabled properties.

This example also uses the GetEventProperty method to attach an event handler.

' This method creates a Timer component using the  
' IDesignerHost.CreateComponent method. It also  
' creates an event handler for the Timer component's 
' tick event. 
Private Sub CreateTimer()
    If (Me.host IsNot Nothing) Then 
        If Me.relatedDesigner.createdTimer Is Nothing Then 
            ' Create and configure the Timer object. 
            Me.relatedDesigner.createdTimer = Me.host.CreateComponent(GetType(Timer))

            Dim t As Timer = Me.relatedDesigner.createdTimer
            t.Interval = 1000
            t.Enabled = True 

            Dim eventColl As EventDescriptorCollection = TypeDescriptor.GetEvents(t, New Attribute(-1) {})

            If (eventColl IsNot Nothing) Then 
                Dim ed As EventDescriptor = eventColl("Tick")

                If (ed IsNot Nothing) Then 
                    Dim epd As PropertyDescriptor = Me.relatedDesigner.eventBindingService.GetEventProperty(ed)

                    epd.SetValue(t, "timer_Tick")
                End If 
            End If 

            Me.relatedDesigner.actionUiService.Refresh(Me.relatedControl)
        End If 
    End If 
End Sub
// This method creates a Timer component using the  
        // IDesignerHost.CreateComponent method. It also  
        // creates an event handler for the Timer component's 
        // tick event. 
        private void CreateTimer()
        {
            if (this.host != null)
            {
                if (this.relatedDesigner.createdTimer == null)
                {
                    // Create and configure the Timer object. 
                    this.relatedDesigner.createdTimer =
                        this.host.CreateComponent(typeof(Timer)) as Timer;
                    Timer t = this.relatedDesigner.createdTimer;
                    t.Interval = 1000;
                    t.Enabled = true;

                    EventDescriptorCollection eventColl =
                        TypeDescriptor.GetEvents(t, new Attribute[0]);

                    if (eventColl != null)
                    {
                        EventDescriptor ed =
                            eventColl["Tick"] as EventDescriptor;
                        if (ed != null)
                        {
                            PropertyDescriptor epd =
                                this.relatedDesigner.eventBindingService.GetEventProperty(ed);

                            epd.SetValue(t, "timer_Tick");
                        }
                    }

                    this.relatedDesigner.actionUiService.Refresh(this.relatedControl);
                }
            }
        }

Removing a Component Programmatically

The following code example calls the DestroyComponent method to remove a Timer component from the design environment.

' This method uses the IDesignerHost.DestroyComponent method 
' to remove the Timer component from the design environment. 
Private Sub RemoveTimer()
    If (Me.host IsNot Nothing) Then 
        If (Me.relatedDesigner.createdTimer IsNot Nothing) Then 
            Me.host.DestroyComponent(Me.relatedDesigner.createdTimer)

            Me.relatedDesigner.createdTimer = Nothing 

            Me.relatedDesigner.actionUiService.Refresh(Me.relatedControl)
        End If 
    End If 
End Sub
// This method uses the IDesignerHost.DestroyComponent method 
        // to remove the Timer component from the design environment. 
        private void RemoveTimer()
        {
            if (this.host != null)
            {
                if (this.relatedDesigner.createdTimer != null)
                {
                    this.host.DestroyComponent(
                        this.relatedDesigner.createdTimer);

                    this.relatedDesigner.createdTimer = null;

                    this.relatedDesigner.actionUiService.Refresh(
                        this.relatedControl);
                }
            }
        }

Initializing a Component Created by the Toolbox

You can also configure components when they are created by the Toolbox. You implement a custom ToolboxItem and override the CreateComponentsCore method.

' Toolbox items must be serializable.
<Serializable(), _
System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Name:="FullTrust"), _
System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name:="FullTrust")> _
Class DemoToolboxItem
    Inherits ToolboxItem

    ' The add components dialog in VS looks for a public 
    ' ctor that takes a type. 
    Public Sub New(ByVal toolType As Type)
        MyBase.New(toolType)
    End Sub 'New 


    ' And you must provide this special constructor for serialization. 
    ' If you add additional data to MyToolboxItem that you 
    ' want to serialize, you may override Deserialize and 
    ' Serialize methods to add that data.   
    Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        Deserialize(info, context)
    End Sub 'New 


    ' This implementation sets the new control's Text and  
    ' AutoSize properties. 
    Protected Overrides Function CreateComponentsCore(ByVal host As IDesignerHost, ByVal defaultValues As IDictionary) As IComponent()

        Dim comps As IComponent() = MyBase.CreateComponentsCore(host, defaultValues)

        ' The returned IComponent array contains a single  
        ' component, which is an instance of DemoControl. 
        CType(comps(0), DemoControl).Text = "This text was set by CreateComponentsCore." 
        CType(comps(0), DemoControl).AutoSize = True 

        Return comps
    End Function 
End Class
// Toolbox items must be serializable.
    [Serializable]
    [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Name="FullTrust")]
    [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name="FullTrust")]
    class DemoToolboxItem : ToolboxItem
    {
        // The add components dialog in VS looks for a public 
        // ctor that takes a type. 
        public DemoToolboxItem(Type toolType)
            : base(toolType)
        {
        }

        // And you must provide this special constructor for serialization. 
        // If you add additional data to MyToolboxItem that you 
        // want to serialize, you may override Deserialize and 
        // Serialize methods to add that data.  
        DemoToolboxItem(SerializationInfo info, StreamingContext context)
        {
            Deserialize(info, context);
        }

        // This implementation sets the new control's Text and  
        // AutoSize properties. 
        protected override IComponent[] CreateComponentsCore(
            IDesignerHost host, 
            IDictionary defaultValues)
        {

            IComponent[] comps = base.CreateComponentsCore(host, defaultValues);

            // The returned IComponent array contains a single  
            // component, which is an instance of DemoControl.
            ((DemoControl)comps[0]).Text = "This text was set by CreateComponentsCore.";
            ((DemoControl)comps[0]).AutoSize = true;

            return comps;
        }
    }

Attach your component to your ToolboxItem class by using ToolboxItemAttribute.

<DesignerAttribute(GetType(DemoControlDesigner)), _
ToolboxItem(GetType(DemoControl.DemoToolboxItem))> _
Public Class DemoControl
    Inherits Label
[DesignerAttribute(typeof(DemoControlDesigner))]
[ToolboxItem(typeof(DemoToolboxItem))]
public class DemoControl : Label
{

Compiling the Code

When you make changes to the design-time aspects of a component, you need to rebuild the control project. In addition, if there is another Windows Forms project that is currently open and uses this component, you will probably need to refresh the project to see the changes. Typically, you need to close and reopen the design window containing the component.

See Also

Tasks

How to: Access Design-Time Support in Windows Forms

How to: Extend the Appearance and Behavior of Controls in Design Mode

How to: Perform Custom Initialization for Controls in Design Mode