This documentation is archived and is not being maintained.

ToolboxItemCreatorCallback Delegate

Provides a callback mechanism that can create a ToolboxItem.

Namespace:  System.Drawing.Design
Assembly:  System.Drawing (in System.Drawing.dll)

'Declaration
Public Delegate Function ToolboxItemCreatorCallback ( _
	serializedObject As Object, _
	format As String _
) As ToolboxItem
'Usage
Dim instance As New ToolboxItemCreatorCallback(AddressOf HandlerMethod)

Parameters

serializedObject
Type: System.Object

The object which contains the data to create a ToolboxItem for.

format
Type: System.String

Return Value

Type: System.Drawing.Design.ToolboxItem
The deserialized ToolboxItem object specified by serializedObject.

You can implement a toolbox item creator method with a method signature matching the method signature of this delegate type that creates a toolbox item from any object of a particular clipboard data format placed on the toolbox. For example, you can design a toolbox item creator that creates a TextBox to store text pasted to the toolbox from the clipboard. You can use the AddCreator method of the IToolboxService to add a ToolboxItemCreatorCallback event handler for a particular data type to the toolbox. The serializedObject parameter contains the data object.

When you create a ToolboxItemCreatorCallback delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Events and Delegates.

The following example provides a component that uses the IToolboxService to add a "Text" data format handler, or ToolboxItemCreatorCallback, to the toolbox. The data creator callback delegate passes any text data pasted to the toolbox and dragged onto a form to a custom ToolboxItem that creates a TextBox containing the text.

Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Security.Permissions
Imports System.Windows.Forms

' Component that adds a "Text" data format ToolboxItemCreatorCallback  
' to the Toolbox. This component uses a custom ToolboxItem that  
' creates a TextBox containing the text data.
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Class TextDataTextBoxComponent
    Inherits System.ComponentModel.Component

    Private creatorAdded As Boolean = False 
    Private ts As IToolboxService

    Public Sub New()
    End Sub 

    ' ISite override to register TextBox creator 
    Public Overrides Property Site() As System.ComponentModel.ISite
        Get 
            Return MyBase.Site
        End Get 
        Set(ByVal Value As System.ComponentModel.ISite)
            If (Value IsNot Nothing) Then 
                MyBase.Site = Value
                If Not creatorAdded Then
                    AddTextTextBoxCreator()
                End If 
            Else 
                If creatorAdded Then
                    RemoveTextTextBoxCreator()
                End If 
                MyBase.Site = Value
            End If 
        End Set 
    End Property 

    ' Adds a "Text" data format creator to the toolbox that creates  
    ' a textbox from a text fragment pasted to the toolbox. 
    Private Sub AddTextTextBoxCreator()
        ts = CType(GetService(GetType(IToolboxService)), IToolboxService)
        If (ts IsNot Nothing) Then 
            Dim textCreator As _
            New ToolboxItemCreatorCallback(AddressOf Me.CreateTextBoxForText)
            Try
                ts.AddCreator( _
                textCreator, _
                "Text", _
                CType(GetService(GetType(IDesignerHost)), IDesignerHost))

                creatorAdded = True 
            Catch ex As Exception
                MessageBox.Show(ex.ToString(), "Exception Information")
            End Try 
        End If 
    End Sub 

    ' Removes any "Text" data format creator from the toolbox. 
    Private Sub RemoveTextTextBoxCreator()
        If (ts IsNot Nothing) Then
            ts.RemoveCreator( _
            "Text", _
            CType(GetService(GetType(IDesignerHost)), IDesignerHost))
            creatorAdded = False 
        End If 
    End Sub 

    ' ToolboxItemCreatorCallback delegate format method to create  
    ' the toolbox item. 
    Private Function CreateTextBoxForText( _
    ByVal serializedObject As Object, _
    ByVal format As String) As ToolboxItem

        Dim o As New DataObject(CType(serializedObject, IDataObject))

        Dim formats As String() = o.GetFormats()

        If o.GetDataPresent("System.String", True) Then 

            Return New TextToolboxItem(CStr(o.GetData("System.String", True)))

        End If 

        Return Nothing 
    End Function 

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If creatorAdded Then
            RemoveTextTextBoxCreator()
        End If 
    End Sub 

End Class 

' Custom toolbox item creates a TextBox and sets its Text property 
' to the constructor-specified text.
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Class TextToolboxItem
    Inherits System.Drawing.Design.ToolboxItem

    Private [text] As String 

    Delegate Sub SetTextMethodHandler( _
    ByVal c As Control, _
    ByVal [text] As String)

    Public Sub New(ByVal [text] As String)
        Me.text = [text]
    End Sub 

    ' ToolboxItem.CreateComponentsCore override to create the TextBox  
    ' and link a method to set its Text property.
    <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Function CreateComponentsCore(ByVal host As IDesignerHost) As IComponent()
        Dim textbox As TextBox = CType(host.CreateComponent(GetType(TextBox)), TextBox)

        ' Because the designer resets the text of the textbox, use  
        ' a SetTextMethodHandler to set the text to the value of  
        ' the text data. 
        Dim c As Control = host.RootComponent

        c.BeginInvoke( _
        New SetTextMethodHandler(AddressOf OnSetText), _
        New Object() {textbox, [text]})

        Return New System.ComponentModel.IComponent() {textbox}
    End Function 

    ' Method to set the text property of a TextBox after it is initialized. 
    Private Sub OnSetText(ByVal c As Control, ByVal [text] As String)
        c.Text = [text]
    End Sub 

End Class

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: