次の方法で共有


ToolboxItem クラス

ツールボックス項目の基本の実装を提供します。

この型のすべてのメンバの一覧については、ToolboxItem メンバ を参照してください。

System.Object
   System.Drawing.Design.ToolboxItem
      System.Web.UI.Design.WebControlToolboxItem

<Serializable>
Public Class ToolboxItem   Implements ISerializable
[C#]
[Serializable]
public class ToolboxItem : ISerializable
[C++]
[Serializable]
public __gc class ToolboxItem : public ISerializable
[JScript]
public
   Serializable
class ToolboxItem implements ISerializable

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

ToolboxItem は、ツールボックス項目の基本クラスで、デザイン時環境のツールボックスに表示できます。通常、ツールボックス項目は、デザインモードのドキュメントで呼び出して作成に使用するコンポーネントを表します。 ToolboxItem クラスには、ツールボックスに対してツールボックス項目の表示プロパティを提供する、コンポーネントを作成する、および自身の永続化のためにツールボックス データベースにシリアル化または逆シリアル化するために必要なメソッドとプロパティが用意されています。

ToolboxItem クラスのインスタンスは、名前、ビットマップ、およびビットマップを指定して設定できます。 ToolboxItem から派生するクラスを作成する必要はありません。また、 ToolboxItem クラスは、カスタム ツールボックス項目を実装する際の基本クラスも提供します。カスタム ToolboxItem は、複数のコンポーネントを作成できます。カスタム ツールボックス項目を実装するには、 ToolboxItem から派生させて、 CreateComponentsCoreSerialize 、および Deserialize の各メソッドをオーバーライドする必要があります。

ToolboxItem が正常に機能するためには、次のプロパティとメソッドを設定する必要があります。

  • DisplayName プロパティは、ツールボックス項目がツールボックス内に表示されるときのラベルを指定します。
  • TypeName プロパティは、項目によって作成される型のコンポーネントの完全限定名を指定します。派生クラスが複数のコンポーネントを作成する場合、 TypeName プロパティを使用できるかどうかは、 CreateComponentsCore メソッド オーバーライドがこのプロパティの値に依存するかどうかによって決まります。
  • AssemblyName プロパティは、項目によって作成される型のコンポーネントを含むアセンブリを指定します。
  • Bitmap プロパティはオプションで、ツールボックス内でツールボックス項目名の横に表示するビットマップ イメージを指定します。
  • Filter プロパティはオプションで、ツールボックス項目が特定のコンポーネントで使用できるかどうかを決定する ToolboxItemFilterAttribute オブジェクトを含みます。
  • CreateComponentsCore メソッドは、このツールが使用される場所に挿入するコンポーネントのインスタンスを返します。
  • Serialize メソッドは、ツールボックス項目を指定した SerializationInfo に保存します。
  • Deserialize メソッドは、指定した SerializationInfo に含まれる状態情報に基づいて、ツールボックス項目を設定します。
  • CreateComponentsCore メソッドが異なる動作をするようにオーバーライドされていない場合、 Initialize メソッドは、ツールボックス項目を設定して、指定した型のコンポーネントを作成します。
  • Locked プロパティは、ツールボックス項目のプロパティが変更できるかどうかを示します。通常、ツールボックス項目はツールボックスに追加された後はロックされます。
  • Lock メソッドは、ツールボックス項目をロックします。
  • CheckUnlocked メソッドは、 Locked プロパティが true の場合に例外をスローします。

使用例

[Visual Basic, C#, C++] IToolboxService を使用して、ツールボックスに、"Text" データ形式のハンドラを追加したり、 ToolboxItemCreatorCallback を実行したりするコンポーネントを提供する方法を次の例に示します。データ クリエータのコールバック デリゲートは、テキスト データを渡してツールボックスに貼り付け、テキストを格納する System.Windows.Forms.TextBox を作成するカスタム ToolboxItem のフォームにそのテキスト データをドラッグします。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Drawing.Design

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Drawing (System.Drawing.dll 内)

参照

ToolboxItem メンバ | System.Drawing.Design 名前空間 | ToolboxItemFilterAttribute | ToolboxItemAttribute | IToolboxService | IToolboxUser | ToolboxComponentsCreatedEventArgs | ToolboxComponentsCreatedEventHandler