Freigeben über


ToolboxItemCreatorCallback-Delegat

Stellt die Methode dar, die das ToolboxItemCreatorCallback-Ereignis behandelt.

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

Syntax

'Declaration
Public Delegate Function ToolboxItemCreatorCallback ( _
    serializedObject As Object, _
    format As String _
) As ToolboxItem
'Usage
Dim instance As New ToolboxItemCreatorCallback(AddressOf HandlerMethod)
public delegate ToolboxItem ToolboxItemCreatorCallback (
    Object serializedObject,
    string format
)
public delegate ToolboxItem^ ToolboxItemCreatorCallback (
    Object^ serializedObject, 
    String^ format
)
/** @delegate */
public delegate ToolboxItem ToolboxItemCreatorCallback (
    Object serializedObject, 
    String format
)
JScript unterstützt die Verwendung von Delegaten, aber nicht die Deklaration von neuen Delegaten.

Parameter

  • serializedObject
    Das Objekt, das die Daten enthält, für die ein ToolboxItem erstellt werden soll.
  • format

Rückgabewert

Das von serializedObject angegebene deserialisierte ToolboxItem-Objekt.

Hinweise

Sie können eine Toolbox-Elementerstellungsmethode mit einer Methodensignatur implementieren, die mit der Methodensignatur dieses Delegattyps übereinstimmt, der ein Toolboxelement aus einem Objekt eines bestimmten Zwischenablage-Datenformats in der Toolbox erstellt. Sie können z. B. einen Toolbox-Elementersteller entwerfen, der eine TextBox zum Speichern von Text erstellt, der aus der Zwischenablage in die Toolbox kopiert wird. Mit der AddCreator-Methode des IToolboxService können Sie der Toolbox einen ToolboxItemCreatorCallback-Ereignishandler für einen bestimmten Datentyp hinzufügen. Der serializedObject-Parameter enthält das Datenobjekt.

Beim Erstellen eines ToolboxItemCreatorCallback-Delegaten bestimmen Sie die Methode für die Ereignisbehandlung. Um dem Ereignishandler das Ereignis zuzuordnen, fügen Sie dem Ereignis eine Instanz des Delegaten hinzu. Der Ereignishandler wird bei jedem Eintreten des Ereignisses aufgerufen, sofern der Delegat nicht entfernt wird. Weitere Informationen über Ereignishandlerdelegaten finden Sie unter Ereignisse und Delegaten.

Beispiel

Im folgenden Beispiel wird eine Komponente bereitgestellt, die mit IToolboxService einen Text-Datenformathandler oder ToolboxItemCreatorCallback zur Toolbox hinzufügt. Der Datenersteller-Rückrufdelegat übergibt alle an die Toolbox übergebenen und in ein Formular gezogenen Textdaten an ein benutzerdefiniertes ToolboxItem, das eine TextBox mit dem Text erstellt.

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.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.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 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.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.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.
    <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
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.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    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.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
    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;
        }
    }
}
#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;
using namespace System::Security::Permissions;

namespace TextDataTextBoxComponent
{
   // Custom toolbox item creates a TextBox and sets its Text property
   // to the constructor-specified text.
   [PermissionSetAttribute(SecurityAction::Demand, Name="FullTrust")]
   public ref 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;
      }

   protected:

      // ToolboxItem::CreateComponentsCore  to create the TextBox
      // and link a method to set its Text property.

      virtual array<System::ComponentModel::IComponent^>^ CreateComponentsCore( System::ComponentModel::Design::IDesignerHost^ host ) override
      {
         System::Windows::Forms::TextBox^ textbox = dynamic_cast<TextBox^>(host->CreateComponent( TextBox::typeid ));
         
         // 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);
         array<Object^>^temp0 = {textbox,text};
         c->BeginInvoke( gcnew SetTextMethodHandler( this, &TextToolboxItem::OnSetText ), temp0 );
         array<System::ComponentModel::IComponent^>^temp1 = {textbox};
         return temp1;
      }

   private:

      // Method to set the text property of a TextBox after it is initialized.
      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 ref class TextDataTextBoxComponent: public System::ComponentModel::Component
   {
   private:
      bool creatorAdded;
      IToolboxService^ ts;

   public:
      TextDataTextBoxComponent()
      {
         creatorAdded = false;
      }


      property System::ComponentModel::ISite^ Site 
      {
         // ISite to register TextBox creator
         virtual System::ComponentModel::ISite^ get() override
         {
            return __super::Site;
         }

         virtual void set( System::ComponentModel::ISite^ value ) override
         {
            if ( value != nullptr )
            {
               __super::Site = value;
               if (  !creatorAdded )
                              AddTextTextBoxCreator();
            }
            else
            {
               if ( creatorAdded )
                              RemoveTextTextBoxCreator();
               __super::Site = value;
            }
         }
      }

   private:

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


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


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

         return nullptr;
      }

   public:
      ~TextDataTextBoxComponent()
      {
         if ( creatorAdded )
                  RemoveTextTextBoxCreator();
      }
   };
}
package TextDataTextBoxComponent;

import System.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.*;
import System.Drawing.*;
import System.Drawing.Design.*;
import 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 extends System.ComponentModel.Component
{
    private boolean creatorAdded = false;
    private IToolboxService ts;

    public TextDataTextBoxComponent()
    {
    } //TextDataTextBoxComponent

    // ISite override to register TextBox creator
    /** @property 
     */
    public System.ComponentModel.ISite get_Site()
    {
        return super.get_Site();
    } //get_Site

    /** @property 
     */
    public void set_Site(System.ComponentModel.ISite value)
    {
        if (value != null) {
            super.set_Site(value);
            if (!(creatorAdded)) {
                AddTextTextBoxCreator();
            }
        }
        else {
            if (creatorAdded) {
                RemoveTextTextBoxCreator();
            }
            super.set_Site(value);
        }
    } //set_Site

    // 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(IToolboxService.class.ToType());
        if (ts != null) {
            ToolboxItemCreatorCallback textCreator =
                new ToolboxItemCreatorCallback(this.CreateTextBoxForText);
            try {
                ts.AddCreator(textCreator, "Text",
                    (IDesignerHost)GetService(IDesignerHost.class.ToType()));
                creatorAdded = true;
            }
            catch (System.Exception ex) {
                MessageBox.Show(ex.ToString(), "Exception Information");
            }
        }
    } //AddTextTextBoxCreator

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

    // 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;
    } //CreateTextBoxForText

    protected void Dispose(boolean disposing)
    {
        if (creatorAdded) {
            RemoveTextTextBoxCreator();
        }
    } //Dispose
} //TextDataTextBoxComponent

// Custom toolbox item creates a TextBox and sets its Text property
// to the constructor-specified text.
public class TextToolboxItem extends System.Drawing.Design.ToolboxItem
{
    private String text;

    /** @delegate 
     */
    private delegate void SetTextMethodHandler(Control c, String text);

    /** @attribute System.Security.Permissions.PermissionSet(
        System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")
     */
    public TextToolboxItem(String text)
    {
        this.text = text;
    } //TextToolboxItem

    // ToolboxItem.CreateComponentsCore override to create the TextBox 
    // and link a method to set its Text property.
    /** @attribute System.Security.Permissions.PermissionSet(
        System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")
     */
    protected System.ComponentModel.IComponent[] CreateComponentsCore(
        System.ComponentModel.Design.IDesignerHost host)
    {
        System.Windows.Forms.TextBox textbox =
            (TextBox)host.CreateComponent(TextBox.class.ToType());
        // 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 = (Control)host.get_RootComponent();
        c.BeginInvoke(new SetTextMethodHandler(OnSetText),
            new Object[] { textbox, text });

        return new System.ComponentModel.IComponent[] { textbox };
    } //CreateComponentsCore

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

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

System.Drawing.Design-Namespace