ToolboxItemCreatorCallback Delegate

ToolboxItemCreatorCallback Delegate

Represents the method that will handle the ToolboxItemCreatorCallback event.

[Visual Basic]
<Serializable>
Public Delegate Function Sub ToolboxItemCreatorCallback( _
   ByVal serializedObject As Object, _
   ByVal format As String _
) As ToolboxItem
[C#]
[Serializable]
public delegate ToolboxItem ToolboxItemCreatorCallback(
   object serializedObject,
   string format
);
[C++]
[Serializable]
public __gc __delegate ToolboxItem* ToolboxItemCreatorCallback(
   Object* serializedObject,
   String* format
);

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your callback method must have the same parameters as the ToolboxItemCreatorCallback delegate declaration.

serializedObject
The object which contains the data to create a ToolboxItem for.
format
The name of the clipboard data format to create a ToolboxItem for.

Remarks

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.

Example

[Visual Basic, C#, C++] 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 System.Windows.Forms.TextBox containing the text.

[Visual Basic] 
Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Windows.Forms

' Component that adds a "Text" data format ToolboxItemCreatorCallback 
' to the Toolbox that creates a custom ToolboxItem that 
' creates a TextBox containing the text data.
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 Not (Value Is 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 Not (ts Is 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 Not (ts Is 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 formats As String() = CType(serializedObject, System.Windows.Forms.DataObject).GetFormats()
        If CType(serializedObject, System.Windows.Forms.DataObject).GetDataPresent("System.String", True) Then
            Return New TextToolboxItem(CStr(CType(serializedObject, System.Windows.Forms.DataObject).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.
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.
    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Function CreateComponentsCore(ByVal host As System.ComponentModel.Design.IDesignerHost) As System.ComponentModel.IComponent()
        Dim textbox As System.Windows.Forms.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

[C#] 
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;

namespace TextDataTextBoxComponent
{
    // Component that adds a "Text" data format ToolboxItemCreatorCallback 
    // to the Toolbox that creates a custom ToolboxItem that 
    // creates a TextBox containing the text data.
    public class TextDataTextBoxComponent : System.ComponentModel.Component
    {
        private bool creatorAdded = false;
        private IToolboxService ts;

        public TextDataTextBoxComponent()
        {                     
        }

        // ISite override to register TextBox creator
        public override System.ComponentModel.ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                if( value != null )
                {                    
                    base.Site = value;
                    if( !creatorAdded )
                        AddTextTextBoxCreator();                    
                }
                else
                {
                    if( creatorAdded )
                        RemoveTextTextBoxCreator();
                    base.Site = value;             
                }
            }
        }

        // Adds a "Text" data format creator to the toolbox that creates 
        // a textbox from a text fragment pasted to the toolbox.
        private void AddTextTextBoxCreator()
        {
            ts = (IToolboxService)GetService(typeof(IToolboxService));
            if (ts != null) 
            {
                ToolboxItemCreatorCallback textCreator = new ToolboxItemCreatorCallback(this.CreateTextBoxForText);
                try
                {
                    ts.AddCreator(textCreator, "Text", (IDesignerHost)GetService(typeof(IDesignerHost)));
                    creatorAdded = true;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Exception Information");
                }                
            }
        }

        // Removes any "Text" data format creator from the toolbox.
        private void RemoveTextTextBoxCreator()
        {
            if (ts != null)             
            {
                ts.RemoveCreator("Text", (IDesignerHost)GetService(typeof(IDesignerHost)));            
                creatorAdded = false;
            }
        }

        // ToolboxItemCreatorCallback delegate format method to create 
        // the toolbox item.
        private ToolboxItem CreateTextBoxForText(object serializedObject, string format)
        {
            string[] formats = ((System.Windows.Forms.DataObject)serializedObject).GetFormats();
            if( ((System.Windows.Forms.DataObject)serializedObject).GetDataPresent("System.String", true) )           
                return new TextToolboxItem( (string)((System.Windows.Forms.DataObject)serializedObject).GetData("System.String", true) );            
            return null;
        }

        protected override void Dispose(bool disposing)
        {
            if( creatorAdded )
                RemoveTextTextBoxCreator();
        }        
    }

    // Custom toolbox item creates a TextBox and sets its Text property
    // to the constructor-specified text.
    public class TextToolboxItem : System.Drawing.Design.ToolboxItem
    {
        private string text;
        private delegate void SetTextMethodHandler(Control c, string text);

        public TextToolboxItem(string text) : base()
        {
            this.text = text;
        }

        // ToolboxItem.CreateComponentsCore override to create the TextBox 
        // and link a method to set its Text property.
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
        protected override System.ComponentModel.IComponent[] CreateComponentsCore(System.ComponentModel.Design.IDesignerHost host)
        {
            System.Windows.Forms.TextBox textbox = (TextBox)host.CreateComponent(typeof(TextBox));
                
            // Because the designer resets the text of the textbox, use 
            // a SetTextMethodHandler to set the text to the value of 
            // the text data.
            Control c = host.RootComponent as Control;
            c.BeginInvoke(new SetTextMethodHandler(OnSetText), new object[] {textbox, text});
           
            return new System.ComponentModel.IComponent[] { textbox };
        }        

        // Method to set the text property of a TextBox after it is initialized.
        private void OnSetText(Control c, string text) 
        {
            c.Text = text;
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Drawing;
using namespace System::Drawing::Design;
using namespace System::Windows::Forms;

namespace TextDataTextBoxComponent {

   // Custom toolbox item creates a TextBox and sets its Text property
   // to the constructor-specified text.
public __gc class TextToolboxItem : public System::Drawing::Design::ToolboxItem {
private:
   String*  text;
   __delegate void SetTextMethodHandler(Control* c, String*  text);

public:
   TextToolboxItem(String* text) : ToolboxItem() {
      this->text = text;
   }

   // ToolboxItem::CreateComponentsCore  to create the TextBox
   // and link a method to set its Text property.
protected:
   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
   System::ComponentModel::IComponent* CreateComponentsCore(System::ComponentModel::Design::IDesignerHost* host)[] {
      System::Windows::Forms::TextBox* textbox = dynamic_cast<TextBox*>(host->CreateComponent(__typeof(TextBox)));

      // Because the designer resets the text of the textbox, use
      // a SetTextMethodHandler to set the text to the value of
      // the text data.
      Control* c = dynamic_cast<Control*>(host->RootComponent);

      Object* temp0 [] = {textbox, text};
      c->BeginInvoke(new SetTextMethodHandler(this, &TextToolboxItem::OnSetText), temp0);

      System::ComponentModel::IComponent* temp1 [] = {textbox};
      return temp1;
   }

   // Method to set the text property of a TextBox after it is initialized.
private:
   void OnSetText(Control* c, String* text) {
      c->Text = text;
   }
};

// Component that adds a "Text" data format ToolboxItemCreatorCallback
// to the Toolbox that creates a custom ToolboxItem that
// creates a TextBox containing the text data.
public __gc class TextDataTextBoxComponent : public System::ComponentModel::Component {
private:
   bool  creatorAdded;
   IToolboxService*  ts;

public:
   TextDataTextBoxComponent() {
      creatorAdded = false;
   }

   // ISite to register TextBox creator
   __property System::ComponentModel::ISite* get_Site() {
      return Component::get_Site();
   }
   __property void set_Site(System::ComponentModel::ISite* value) {
      if (value != 0) {
         Component::set_Site(value);
         if (!creatorAdded)
            AddTextTextBoxCreator();
      } else {
         if (creatorAdded)
            RemoveTextTextBoxCreator();
         Component::set_Site(value);
      }
   }

   // Adds a "Text" data format creator to the toolbox that creates
   // a textbox from a text fragment pasted to the toolbox.
private:
   void AddTextTextBoxCreator() {
      ts = dynamic_cast<IToolboxService*>(GetService(__typeof(IToolboxService)));
      if (ts != 0) {
         ToolboxItemCreatorCallback* textCreator = new ToolboxItemCreatorCallback(this, &TextDataTextBoxComponent::CreateTextBoxForText);
         try {
            ts->AddCreator(textCreator, S"Text", dynamic_cast<IDesignerHost*>(GetService(__typeof(IDesignerHost))));
            creatorAdded = true;
         } catch (Exception* ex) {
            MessageBox::Show(ex->ToString(), S"Exception Information");
         }
      }
   }

   // Removes any "Text" data format creator from the toolbox.
   void RemoveTextTextBoxCreator() {
      if (ts != 0) {
         ts->RemoveCreator(S"Text", dynamic_cast<IDesignerHost*>(GetService(__typeof(IDesignerHost))));
         creatorAdded = false;
      }
   }

   // ToolboxItemCreatorCallback delegate format method to create
   // the toolbox item.
   ToolboxItem* CreateTextBoxForText(Object* serializedObject, String* format) {
      String* formats[] = (dynamic_cast<System::Windows::Forms::DataObject*>(serializedObject))->GetFormats();
      if ((dynamic_cast<System::Windows::Forms::DataObject*>(serializedObject))->GetDataPresent(S"System::String", true))
         return new TextToolboxItem(dynamic_cast<String*>((dynamic_cast<System::Windows::Forms::DataObject*>(serializedObject))->GetData(S"System::String", true)));
      return 0;
   }

protected:
   void Dispose(bool disposing) {
      if (creatorAdded)
         RemoveTextTextBoxCreator();
   }
};

}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Drawing.Design

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

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

See Also

System.Drawing.Design Namespace

Show:
© 2016 Microsoft