This documentation is archived and is not being maintained.

IToolboxService Interface

Provides methods and properties to manage and query the toolbox in the development environment.

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

'Declaration
<GuidAttribute("4BACD258-DE64-4048-BC4E-FEDBEF9ACB76")> _
<InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IToolboxService
'Usage
Dim instance As IToolboxService

The IToolboxService interface provides properties and methods for adding and removing toolbox items and toolbox creator callback delegates, serializing and deserializing toolbox items, and retrieving toolbox state information and managing toolbox state.

You can retrieve information about the contents of the toolbox with the following methods:

You can add and remove toolbox items with the following methods:

You can refresh the toolbox, mark a toolbox item as used, or set the mouse cursor to a cursor that represents the current toolbox item using the following methods:

  • The Refresh method refreshes the toolbox display to reflect the current state of the toolbox items.

  • The SelectedToolboxItemUsed method signals the toolbox that the selected toolbox item has been used.

  • The SetCursor method sets the mouse cursor to a cursor that represents the current toolbox item.

You can use the toolbox to serialize or deserialize a toolbox item using the following methods:

The following code example demonstrates the use of the IToolboxService in design mode to list and select toolbox categories and items, and to create components or controls from toolbox items and add them to a Form. To use the example, compile the code to an assembly, and add a reference to the assembly in a Windows Forms application. If you are using Visual Studio, the IToolboxServiceControl is automatically added to the Toolbox. Create an instance of the IToolboxServiceControl on a form to test its behavior.

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Data
Imports System.Diagnostics
Imports System.Reflection
Imports System.Windows.Forms

' Provides an example control that functions in design mode to  
' demonstrate use of the IToolboxService to list and select toolbox  
' categories and items, and to add components or controls  
' to the parent form using code.
<DesignerAttribute(GetType(WindowMessageDesigner), GetType(IDesigner))> _
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Class IToolboxServiceControl
    Inherits System.Windows.Forms.UserControl

    Private WithEvents listBox1 As System.Windows.Forms.ListBox
    Private listBox2 As System.Windows.Forms.ListBox
    Private toolboxService As IToolboxService = Nothing 
    Private tools As ToolboxItemCollection
    Private controlSpacingMultiplier As Integer 

    Public Sub New()
        InitializeComponent()
        AddHandler listBox2.DoubleClick, AddressOf Me.CreateComponent
        controlSpacingMultiplier = 0
    End Sub 

    ' Obtain or reset IToolboxService reference on each siting of control. 
    Public Overrides Property Site() As System.ComponentModel.ISite
        Get 
            Return MyBase.Site
        End Get 
        Set(ByVal Value As System.ComponentModel.ISite)
            MyBase.Site = Value

            ' If the component was sited, attempt to obtain  
            ' an IToolboxService instance. 
            If (MyBase.Site IsNot Nothing) Then
                toolboxService = CType(Me.GetService(GetType(IToolboxService)), IToolboxService)
                ' If an IToolboxService instance was located, update the  
                ' category list. 
                If (toolboxService IsNot Nothing) Then
                    UpdateLists()
                End If 
            Else
                toolboxService = Nothing 
            End If 
        End Set 
    End Property 

    ' Updates the list of categories and the list of items in the  
    ' selected category. 
    Private Sub UpdateLists()
        If (toolboxService IsNot Nothing) Then 
            RemoveHandler listBox1.SelectedIndexChanged, AddressOf Me.UpdateSelectedCategory
            RemoveHandler listBox2.SelectedIndexChanged, AddressOf Me.UpdateSelectedItem
            listBox1.Items.Clear()
            Dim i As Integer 
            For i = 0 To toolboxService.CategoryNames.Count - 1
                listBox1.Items.Add(toolboxService.CategoryNames(i))
                If toolboxService.CategoryNames(i) = toolboxService.SelectedCategory Then
                    listBox1.SelectedIndex = i
                    tools = toolboxService.GetToolboxItems(toolboxService.SelectedCategory)
                    listBox2.Items.Clear()
                    Dim j As Integer 
                    For j = 0 To tools.Count - 1
                        listBox2.Items.Add(tools(j).DisplayName)
                    Next j
                End If 
            Next i
            AddHandler Me.listBox1.SelectedIndexChanged, AddressOf Me.UpdateSelectedCategory
            AddHandler Me.listBox2.SelectedIndexChanged, AddressOf Me.UpdateSelectedItem
        End If 
    End Sub 

    ' Sets the selected category when a category is clicked in the  
    ' category list. 
    Private Sub UpdateSelectedCategory(ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
        If (toolboxService IsNot Nothing) Then
            toolboxService.SelectedCategory = CStr(listBox1.SelectedItem)
            UpdateLists()
        End If 
    End Sub 

    ' Sets the selected item when an item is clicked in the item list. 
    Private Sub UpdateSelectedItem(ByVal sender As Object, ByVal e As System.EventArgs)
        If (toolboxService IsNot Nothing) Then 
            If listBox1.SelectedIndex <> -1 Then 
                If CStr(listBox1.SelectedItem) = toolboxService.SelectedCategory Then
                    toolboxService.SetSelectedToolboxItem(tools(listBox2.SelectedIndex))
                Else
                    UpdateLists()
                End If 
            End If 
        End If 
    End Sub 

    ' Creates a control from a double-clicked toolbox item and adds  
    ' it to the parent form. 
    Private Sub CreateComponent(ByVal sender As Object, ByVal e As EventArgs)

        ' Obtains an IDesignerHost service from design environment. 
        Dim host As IDesignerHost = CType(Me.GetService(GetType(IDesignerHost)), IDesignerHost)

        ' Gets the project components container. (Windows Forms control  
        ' containment depends on controls collections). 
        Dim container As IContainer = host.Container

        ' Identifies the parent Form. 
        Dim parentForm As System.Windows.Forms.Form = Me.FindForm()

        ' Retrieves the parent Form's designer host. 
        Dim parentHost As IDesignerHost = CType(parentForm.Site.GetService(GetType(IDesignerHost)), IDesignerHost)

        ' Create the components. 
        Dim comps As IComponent() = Nothing 
        Try
            comps = toolboxService.GetSelectedToolboxItem().CreateComponents(parentHost)
        Catch ex As Exception
            ' Catch and show any exceptions to prevent  
            ' disabling the control's UI.
            MessageBox.Show(ex.ToString(), "Exception message")
        End Try 

        If comps Is Nothing Then 
            Return 
        End If 

        ' Add any created controls to the parent form's controls  
        ' collection. Note: components are added from the  
        ' ToolboxItem.CreateComponents(IDesignerHost) method. 
        Dim i As Integer 
        For i = 0 To comps.Length - 1
            If (parentForm IsNot Nothing) AndAlso CType(comps(i), Object).GetType().IsSubclassOf(GetType(System.Windows.Forms.Control)) Then
                CType(comps(i), System.Windows.Forms.Control).Location = New Point(20 * controlSpacingMultiplier, 20 * controlSpacingMultiplier)
                If controlSpacingMultiplier > 10 Then
                    controlSpacingMultiplier = 0
                Else
                    controlSpacingMultiplier += 1
                End If
                parentForm.Controls.Add(CType(comps(i), System.Windows.Forms.Control))
            End If 
        Next i
    End Sub 

    ' Displays labels. 
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        e.Graphics.DrawString("IToolboxService Control", New Font("Arial", 14), New SolidBrush(Color.Black), 6, 4)
        e.Graphics.DrawString("Category List", New Font("Arial", 8), New SolidBrush(Color.Black), 8, 26)
        e.Graphics.DrawString("Items in Category", New Font("Arial", 8), New SolidBrush(Color.Black), 208, 26)
        e.Graphics.DrawString("(Double-click item to add to parent form)", New Font("Arial", 7), New SolidBrush(Color.Black), 232, 12)
    End Sub 

    Private Sub InitializeComponent()
        Me.listBox1 = New System.Windows.Forms.ListBox()
        Me.listBox2 = New System.Windows.Forms.ListBox()
        Me.SuspendLayout()
        Me.listBox1.Anchor = System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left
        Me.listBox1.Location = New System.Drawing.Point(8, 41)
        Me.listBox1.Name = "listBox1" 
        Me.listBox1.Size = New System.Drawing.Size(192, 368)
        Me.listBox1.TabIndex = 0
        Me.listBox2.Anchor = System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right
        Me.listBox2.Location = New System.Drawing.Point(208, 41)
        Me.listBox2.Name = "listBox2" 
        Me.listBox2.Size = New System.Drawing.Size(228, 368)
        Me.listBox2.TabIndex = 3
        Me.BackColor = System.Drawing.Color.Beige
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox2, Me.listBox1})
        Me.Location = New System.Drawing.Point(500, 400)
        Me.Name = "IToolboxServiceControl" 
        Me.Size = New System.Drawing.Size(442, 422)
        Me.ResumeLayout(False)
    End Sub 

End Class 

' This designer passes window messages to the controls at design time.     
Public Class WindowMessageDesigner
    Inherits System.Windows.Forms.Design.ControlDesigner

    Public Sub New()
    End Sub 

    ' Window procedure override passes events to control.
    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.HWnd.Equals(Me.Control.Handle) Then 
            MyBase.WndProc(m)
        Else 
            Me.DefWndProc(m)
        End If 
    End Sub 

End Class

The following code 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: