ToolboxItem イベント
ToolboxItem クラス
アセンブリ: System.Drawing (system.drawing.dll 内)


ToolboxItem は、ツールボックス項目の基本クラスで、デザイン時環境のツールボックスに表示できます。通常、ツールボックス項目は、デザインモードのドキュメントで呼び出して作成に使用するコンポーネントを表します。ToolboxItem クラスには、ツールボックスに対してツールボックス項目の表示プロパティを提供する、コンポーネントを作成する、および自身の永続化のためにツールボックス データベースにシリアル化または逆シリアル化するために必要なメソッドとプロパティが用意されています。
ToolboxItem クラスのインスタンスは、名前、ビットマップ、およびビットマップを指定して設定できます。ToolboxItem から派生するクラスを作成する必要はありません。また、ToolboxItem クラスは、カスタム ツールボックス項目を実装する際の基本クラスも提供します。カスタム ToolboxItem は、複数のコンポーネントを作成できます。カスタム ツールボックス項目を実装するには、ToolboxItem から派生させて、CreateComponentsCore、Serialize、および Deserialize の各メソッドをオーバーライドする必要があります。
ToolboxItem が正常に機能するためには、次のプロパティとメソッドを設定する必要があります。
-
TypeName プロパティは、項目によって作成される型のコンポーネントの完全限定名を指定します。派生クラスが複数のコンポーネントを作成する場合、TypeName プロパティを使用できるかどうかは、CreateComponentsCore メソッド オーバーライドがこのプロパティの値に依存するかどうかによって決まります。
-
Bitmap プロパティはオプションで、ツールボックス内でツールボックス項目名の横に表示するビットマップ イメージを指定します。
-
Filter プロパティはオプションで、ツールボックス項目が特定のコンポーネントで使用できるかどうかを決定する ToolboxItemFilterAttribute オブジェクトを含みます。
-
CreateComponentsCore メソッドは、このツールが使用される場所に挿入するコンポーネントのインスタンスを返します。
-
Deserialize メソッドは、指定した SerializationInfo に含まれる状態情報に基づいて、ツールボックス項目を設定します。
-
CreateComponentsCore メソッドが異なる動作をするようにオーバーライドされていない場合、Initialize メソッドは、ツールボックス項目を設定して、指定した型のコンポーネントを作成します。
-
Locked プロパティは、ツールボックス項目のプロパティが変更できるかどうかを示します。通常、ツールボックス項目はツールボックスに追加された後はロックされます。

IToolboxService インターフェイスを使用して、ツールボックスに、テキスト データ形式のハンドラ (ToolboxItemCreatorCallback) を追加するコンポーネントのコード例を次に示します。データ クリエータのコールバック デリゲートは、テキスト データを渡してツールボックスに貼り付け、テキストを格納する 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. <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
基本クラスに ToolboxItem クラスを使用して、カスタム ツールボックス項目を実装するコード例を次に示します。
Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Drawing Imports System.Drawing.Design Imports System.Runtime.Serialization Imports System.Text Imports System.Windows.Forms Public Class Form1 Inherits Form Private components As System.ComponentModel.IContainer = Nothing Friend WithEvents UserControl11 As UserControl1 Friend WithEvents label1 As System.Windows.Forms.Label Public Sub New() InitializeComponent() End Sub Private Sub InitializeComponent() Me.label1 = New System.Windows.Forms.Label Me.UserControl11 = New UserControl1 Me.SuspendLayout() ' 'label1 ' Me.label1.Location = New System.Drawing.Point(15, 16) Me.label1.Name = "label1" Me.label1.Size = New System.Drawing.Size(265, 62) Me.label1.TabIndex = 0 Me.label1.Text = "Build the project and drop UserControl1" + _ " from the toolbox onto the form." ' 'UserControl11 ' Me.UserControl11.LabelText = "This is a custom user control. " + _ "The text you see here is provided by a custom too" + _ "lbox item when the user control is dropped on the form." Me.UserControl11.Location = New System.Drawing.Point(74, 85) Me.UserControl11.Name = "UserControl11" Me.UserControl11.Padding = New System.Windows.Forms.Padding(6) Me.UserControl11.TabIndex = 1 ' 'Form1 ' Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.UserControl11) Me.Controls.Add(Me.label1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso Not (components Is Nothing) Then components.Dispose() End If MyBase.Dispose(disposing) End Sub Shared Sub Main() Application.EnableVisualStyles() Application.Run(New Form1()) End Sub End Class ' Configure this user control to have a custom toolbox item. <ToolboxItem(GetType(MyToolboxItem))> _ Public Class UserControl1 Inherits UserControl Private components As System.ComponentModel.IContainer = Nothing Private label1 As System.Windows.Forms.Label Public Sub New() InitializeComponent() End Sub Public Property LabelText() As String Get Return label1.Text End Get Set(ByVal value As String) label1.Text = Value End Set End Property Private Sub InitializeComponent() Me.label1 = New System.Windows.Forms.Label() Me.SuspendLayout() ' ' label1 ' Me.label1.Dock = System.Windows.Forms.DockStyle.Fill Me.label1.Location = New System.Drawing.Point(6, 6) Me.label1.Name = "label1" Me.label1.Size = New System.Drawing.Size(138, 138) Me.label1.TabIndex = 0 Me.label1.Text = "This is a custom user control. " + _ "The text you see here is provided by a custom toolbox" + _ " item when the user control is dropped on the form." + _ vbCr + vbLf Me.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' ' UserControl1 ' Me.Controls.Add(label1) Me.Name = "UserControl1" Me.Padding = New System.Windows.Forms.Padding(6) Me.ResumeLayout(False) End Sub Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso Not (components Is Nothing) Then components.Dispose() End If MyBase.Dispose(disposing) End Sub End Class ' Toolbox items must be serializable. <Serializable()> _ Class MyToolboxItem Inherits ToolboxItem ' The add components dialog in Visual Studio looks for a public ' constructor that takes a type. Public Sub New(ByVal toolType As Type) MyBase.New(toolType) End Sub ' And you must provide this special constructor for serialization. ' If you add additional data to MyToolboxItem that you ' want to serialize, you may override Deserialize and ' Serialize methods to add that data. Sub New(ByVal info As SerializationInfo, _ ByVal context As StreamingContext) Deserialize(info, context) End Sub ' Let's override the creation code and pop up a dialog. Protected Overrides Function CreateComponentsCore( _ ByVal host As System.ComponentModel.Design.IDesignerHost, _ ByVal defaultValues As System.Collections.IDictionary) _ As IComponent() ' Get the string we want to fill in the custom ' user control. If the user cancels out of the dialog, ' return null or an empty array to signify that the ' tool creation was canceled. Dim d As New ToolboxItemDialog() If d.ShowDialog() = DialogResult.OK Then Dim [text] As String = d.CreationText Dim comps As IComponent() = _ MyBase.CreateComponentsCore(host, defaultValues) ' comps will have a single component: our data type. CType(comps(0), UserControl1).LabelText = [text] Return comps Else Return Nothing End If End Function End Class Public Class ToolboxItemDialog Inherits Form Private components As System.ComponentModel.IContainer = Nothing Private label1 As System.Windows.Forms.Label Private textBox1 As System.Windows.Forms.TextBox Private button1 As System.Windows.Forms.Button Private button2 As System.Windows.Forms.Button Public Sub New() InitializeComponent() End Sub Private Sub InitializeComponent() Me.label1 = New System.Windows.Forms.Label() Me.textBox1 = New System.Windows.Forms.TextBox() Me.button1 = New System.Windows.Forms.Button() Me.button2 = New System.Windows.Forms.Button() Me.SuspendLayout() ' ' label1 ' Me.label1.Location = New System.Drawing.Point(10, 9) Me.label1.Name = "label1" Me.label1.Size = New System.Drawing.Size(273, 43) Me.label1.TabIndex = 0 Me.label1.Text = "Enter the text you would like" + _ " to have the user control populated with:" ' ' textBox1 ' Me.textBox1.AutoSize = False Me.textBox1.Location = New System.Drawing.Point(10, 58) Me.textBox1.Multiline = True Me.textBox1.Name = "textBox1" Me.textBox1.Size = New System.Drawing.Size(270, 67) Me.textBox1.TabIndex = 1 Me.textBox1.Text = "This is a custom user control. " + _ "The text you see here is provided by a custom toolbox" + _ " item when the user control is dropped on the form." ' ' button1 ' Me.button1.DialogResult = System.Windows.Forms.DialogResult.OK Me.button1.Location = New System.Drawing.Point(124, 131) Me.button1.Name = "button1" Me.button1.TabIndex = 2 Me.button1.Text = "OK" ' ' button2 ' Me.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.button2.Location = New System.Drawing.Point(205, 131) Me.button2.Name = "button2" Me.button2.TabIndex = 3 Me.button2.Text = "Cancel" ' ' ToolboxItemDialog ' Me.AcceptButton = Me.button1 Me.CancelButton = Me.button2 Me.ClientSize = New System.Drawing.Size(292, 162) Me.ControlBox = False Me.Controls.Add(button2) Me.Controls.Add(button1) Me.Controls.Add(textBox1) Me.Controls.Add(label1) Me.FormBorderStyle = _ System.Windows.Forms.FormBorderStyle.FixedDialog Me.Name = "ToolboxItemDialog" Me.Text = "ToolboxItemDialog" Me.ResumeLayout(False) End Sub Public ReadOnly Property CreationText() As String Get Return textBox1.Text End Get End Property Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso Not (components Is Nothing) Then components.Dispose() End If MyBase.Dispose(disposing) End Sub End Class
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Design; using System.Data; using System.Runtime.Serialization; using System.Text; using System.Windows.Forms; namespace CustomToolboxItem { public class Form1 : Form { private System.ComponentModel.IContainer components = null; private UserControl1 userControl11; private System.Windows.Forms.Label label1; public Form1() { InitializeComponent(); } private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.userControl11 = new CustomToolboxItem.UserControl1(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(15, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(265, 62); this.label1.TabIndex = 0; this.label1.Text = "Build the project and drop UserControl1 from the toolbox onto the form."; // // userControl11 // this.userControl11.LabelText = "AASSDD"; this.userControl11.Location = new System.Drawing.Point(68, 85); this.userControl11.Name = "userControl11"; this.userControl11.Padding = new System.Windows.Forms.Padding(6); this.userControl11.TabIndex = 1; // // Form1 // this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.userControl11); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } } // Configure this user control to have a custom toolbox item. [ToolboxItem(typeof(MyToolboxItem))] public class UserControl1 : UserControl { private System.ComponentModel.IContainer components = null; private System.Windows.Forms.Label label1; public UserControl1() { InitializeComponent(); } public string LabelText { get { return label1.Text; } set { label1.Text = value; } } private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.Dock = System.Windows.Forms.DockStyle.Fill; this.label1.Location = new System.Drawing.Point(6, 6); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(138, 138); this.label1.TabIndex = 0; this.label1.Text = "This is a custom user control. " + "The text you see here is provided by a custom toolbox" + " item when the user control is dropped on the form.\r\n"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // UserControl1 // this.Controls.Add(this.label1); this.Name = "UserControl1"; this.Padding = new System.Windows.Forms.Padding(6); this.ResumeLayout(false); } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } } // Toolbox items must be serializable. [Serializable] class MyToolboxItem : ToolboxItem { // The add components dialog in Visual Studio looks for a public // constructor that takes a type. public MyToolboxItem(Type toolType) : base(toolType) { } // And you must provide this special constructor for serialization. // If you add additional data to MyToolboxItem that you // want to serialize, you may override Deserialize and // Serialize methods to add that data. MyToolboxItem(SerializationInfo info, StreamingContext context) { Deserialize(info, context); } // Let's override the creation code and pop up a dialog. protected override IComponent[] CreateComponentsCore( System.ComponentModel.Design.IDesignerHost host, System.Collections.IDictionary defaultValues) { // Get the string we want to fill in the custom // user control. If the user cancels out of the dialog , // return null or an empty array to signify that the // tool creation was canceled. ToolboxItemDialog d = new ToolboxItemDialog(); if (d.ShowDialog() == DialogResult.OK) { string text = d.CreationText; IComponent[] comps = base.CreateComponentsCore(host, defaultValues); // comps will have a single component: our data type. ((UserControl1)comps[0]).LabelText = text; return comps; } else { return null; } } } public class ToolboxItemDialog : Form { private System.ComponentModel.IContainer components = null; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; public ToolboxItemDialog() { InitializeComponent(); } private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(10, 9); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(273, 43); this.label1.TabIndex = 0; this.label1.Text = "Enter the text you would like" + " to have the user control populated with:"; // // textBox1 // this.textBox1.AutoSize = false; this.textBox1.Location = new System.Drawing.Point(10, 58); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(270, 67); this.textBox1.TabIndex = 1; this.textBox1.Text = "This is a custom user control. " + "The text you see here is provided by a custom toolbox" + " item when the user control is dropped on the form."; // // button1 // this.button1.DialogResult = System.Windows.Forms.DialogResult.OK; this.button1.Location = new System.Drawing.Point(124, 131); this.button1.Name = "button1"; this.button1.TabIndex = 2; this.button1.Text = "OK"; // // button2 // this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.button2.Location = new System.Drawing.Point(205, 131); this.button2.Name = "button2"; this.button2.TabIndex = 3; this.button2.Text = "Cancel"; // // ToolboxItemDialog // this.AcceptButton = this.button1; this.CancelButton = this.button2; this.ClientSize = new System.Drawing.Size(292, 162); this.ControlBox = false; this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Name = "ToolboxItemDialog"; this.Text = "ToolboxItemDialog"; this.ResumeLayout(false); } public string CreationText { get { return textBox1.Text; } } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } } }


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


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ToolboxItem コンストラクタ ()
アセンブリ: System.Drawing (system.drawing.dll 内)


ToolboxItem から派生したクラスに ToolboxItem コンストラクタを使用して、カスタム ツールボックス項目を実装するコード例を次に示します。このコード例は、ToolboxItem クラスのトピックで取り上げているコード例の一部分です。
<Serializable()> _ Class MyToolboxItem Inherits ToolboxItem ' The add components dialog in Visual Studio looks for a public ' constructor that takes a type. Public Sub New(ByVal toolType As Type) MyBase.New(toolType) End Sub ' And you must provide this special constructor for serialization. ' If you add additional data to MyToolboxItem that you ' want to serialize, you may override Deserialize and ' Serialize methods to add that data. Sub New(ByVal info As SerializationInfo, _ ByVal context As StreamingContext) Deserialize(info, context) End Sub ' Let's override the creation code and pop up a dialog. Protected Overrides Function CreateComponentsCore( _ ByVal host As System.ComponentModel.Design.IDesignerHost, _ ByVal defaultValues As System.Collections.IDictionary) _ As IComponent() ' Get the string we want to fill in the custom ' user control. If the user cancels out of the dialog, ' return null or an empty array to signify that the ' tool creation was canceled. Dim d As New ToolboxItemDialog() If d.ShowDialog() = DialogResult.OK Then Dim [text] As String = d.CreationText Dim comps As IComponent() = _ MyBase.CreateComponentsCore(host, defaultValues) ' comps will have a single component: our data type. CType(comps(0), UserControl1).LabelText = [text] Return comps Else Return Nothing End If End Function End Class
// Toolbox items must be serializable. [Serializable] class MyToolboxItem : ToolboxItem { // The add components dialog in Visual Studio looks for a public // constructor that takes a type. public MyToolboxItem(Type toolType) : base(toolType) { } // And you must provide this special constructor for serialization. // If you add additional data to MyToolboxItem that you // want to serialize, you may override Deserialize and // Serialize methods to add that data. MyToolboxItem(SerializationInfo info, StreamingContext context) { Deserialize(info, context); } // Let's override the creation code and pop up a dialog. protected override IComponent[] CreateComponentsCore( System.ComponentModel.Design.IDesignerHost host, System.Collections.IDictionary defaultValues) { // Get the string we want to fill in the custom // user control. If the user cancels out of the dialog, // return null or an empty array to signify that the // tool creation was canceled. ToolboxItemDialog d = new ToolboxItemDialog(); if (d.ShowDialog() == DialogResult.OK) { string text = d.CreationText; IComponent[] comps = base.CreateComponentsCore(host, defaultValues); // comps will have a single component: our data type. ((UserControl1)comps[0]).LabelText = text; return comps; } else { return null; } } }

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ToolboxItem コンストラクタ (Type)
アセンブリ: System.Drawing (system.drawing.dll 内)



ToolboxItem から派生したクラスに ToolboxItem コンストラクタを使用して、カスタム ツールボックス項目を実装するコード例を次に示します。このコード例は、ToolboxItem クラスのトピックで取り上げているコード例の一部分です。
<Serializable()> _ Class MyToolboxItem Inherits ToolboxItem ' The add components dialog in Visual Studio looks for a public ' constructor that takes a type. Public Sub New(ByVal toolType As Type) MyBase.New(toolType) End Sub ' And you must provide this special constructor for serialization. ' If you add additional data to MyToolboxItem that you ' want to serialize, you may override Deserialize and ' Serialize methods to add that data. Sub New(ByVal info As SerializationInfo, _ ByVal context As StreamingContext) Deserialize(info, context) End Sub ' Let's override the creation code and pop up a dialog. Protected Overrides Function CreateComponentsCore( _ ByVal host As System.ComponentModel.Design.IDesignerHost, _ ByVal defaultValues As System.Collections.IDictionary) _ As IComponent() ' Get the string we want to fill in the custom ' user control. If the user cancels out of the dialog, ' return null or an empty array to signify that the ' tool creation was canceled. Dim d As New ToolboxItemDialog() If d.ShowDialog() = DialogResult.OK Then Dim [text] As String = d.CreationText Dim comps As IComponent() = _ MyBase.CreateComponentsCore(host, defaultValues) ' comps will have a single component: our data type. CType(comps(0), UserControl1).LabelText = [text] Return comps Else Return Nothing End If End Function End Class
// Toolbox items must be serializable. [Serializable] class MyToolboxItem : ToolboxItem { // The add components dialog in Visual Studio looks for a public // constructor that takes a type. public MyToolboxItem(Type toolType) : base(toolType) { } // And you must provide this special constructor for serialization. // If you add additional data to MyToolboxItem that you // want to serialize, you may override Deserialize and // Serialize methods to add that data. MyToolboxItem(SerializationInfo info, StreamingContext context) { Deserialize(info, context); } // Let's override the creation code and pop up a dialog. protected override IComponent[] CreateComponentsCore( System.ComponentModel.Design.IDesignerHost host, System.Collections.IDictionary defaultValues) { // Get the string we want to fill in the custom // user control. If the user cancels out of the dialog, // return null or an empty array to signify that the // tool creation was canceled. ToolboxItemDialog d = new ToolboxItemDialog(); if (d.ShowDialog() == DialogResult.OK) { string text = d.CreationText; IComponent[] comps = base.CreateComponentsCore(host, defaultValues); // comps will have a single component: our data type. ((UserControl1)comps[0]).LabelText = text; return comps; } else { return null; } } }

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ToolboxItem コンストラクタ

名前 | 説明 |
---|---|
ToolboxItem () | ToolboxItem クラスの新しいインスタンスを初期化します。 |
ToolboxItem (Type) | 指定した型のコンポーネントを作成する ToolboxItem クラスの新しいインスタンスを初期化します。 |

ToolboxItem プロパティ

名前 | 説明 | |
---|---|---|
![]() | AssemblyName | ツールボックス項目が作成する型が含まれるアセンブリの名前を取得または設定します。 |
![]() | Bitmap | ツールボックスでツールボックス項目を表すビットマップを取得または設定します。 |
![]() | Company | この ToolboxItem の会社名を取得または設定します。 |
![]() | ComponentType | この ToolboxItem のコンポーネントの型を取得します。 |
![]() | DependentAssemblies | ツールボックス項目の AssemblyName を取得または設定します。 |
![]() | Description | この ToolboxItem の説明を取得または設定します。 |
![]() | DisplayName | ツールボックス項目の表示名を取得または設定します。 |
![]() | Filter | ツールボックス項目が対象のコンポーネントで使用できるかどうかを決定するフィルタを取得または設定します。 |
![]() | IsTransient | ツールボックス項目が遷移的かどうかを示す値を取得します。 |
![]() | Locked | ToolboxItem がロックされているかどうかを示す値を取得します。 |
![]() | Properties | プロパティのディクショナリを取得します。 |
![]() | TypeName | ツールボックス項目が呼び出されたときに作成する IComponent の型の完全限定名を取得または設定します。 |
![]() | Version | この ToolboxItem のバージョンを取得します。 |

ToolboxItem メソッド

名前 | 説明 | |
---|---|---|
![]() | CreateComponents | オーバーロードされます。 ツールボックス項目で作成するように設定されているコンポーネントを作成します。 |
![]() | Equals | オーバーロードされます。 オーバーライドされます。 2 つの ToolboxItem インスタンスが等しいかどうかを判断します。 |
![]() | GetHashCode | オーバーライドされます。 このインスタンスのハッシュ コードを返します。 |
![]() | GetType | オーバーロードされます。 指定した型のインスタンスを作成します。 |
![]() | Initialize | 作成する型を指定して、現在のツールボックス項目を初期化します。 |
![]() | Lock | ツールボックス項目をロックして、プロパティが変更されないようにします。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | オーバーライドされます。 現在の ToolboxItem を表す String を返します。 |

名前 | 説明 | |
---|---|---|
![]() | CheckUnlocked | ツールボックス項目が現在ロックされている場合は、例外がスローされます。 |
![]() | CreateComponentsCore | オーバーロードされます。 ツールボックス項目が呼び出されたときに、コンポーネントまたはコンポーネントの配列を作成します。 |
![]() | Deserialize | 指定したシリアル化情報オブジェクトから、このツールボックス項目の状態を読み込みます。 |
![]() | FilterPropertyValue | プロパティ値を返す前に、フィルタ処理をします。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | GetType | オーバーロードされます。 指定した型のインスタンスを作成します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
![]() | OnComponentsCreated | ComponentsCreated イベントを発生させます。 |
![]() | OnComponentsCreating | ComponentsCreating イベントを発生させます。 |
![]() | Serialize | 指定したシリアル化情報オブジェクトに、このツールボックス項目の状態を保存します。 |
![]() | ValidatePropertyType | オブジェクトが特定の型であるかどうかを検証します。 |
![]() | ValidatePropertyValue | プロパティ ディクショナリに割り当てる前にプロパティを検証します。 |

名前 | 説明 | |
---|---|---|
![]() | System.Runtime.Serialization.ISerializable.GetObjectData | このメンバの説明については、GetObjectData メソッドに関するトピックを参照してください。 |

ToolboxItem メンバ
ToolboxItem データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | AssemblyName | ツールボックス項目が作成する型が含まれるアセンブリの名前を取得または設定します。 |
![]() | Bitmap | ツールボックスでツールボックス項目を表すビットマップを取得または設定します。 |
![]() | Company | この ToolboxItem の会社名を取得または設定します。 |
![]() | ComponentType | この ToolboxItem のコンポーネントの型を取得します。 |
![]() | DependentAssemblies | ツールボックス項目の AssemblyName を取得または設定します。 |
![]() | Description | この ToolboxItem の説明を取得または設定します。 |
![]() | DisplayName | ツールボックス項目の表示名を取得または設定します。 |
![]() | Filter | ツールボックス項目が対象のコンポーネントで使用できるかどうかを決定するフィルタを取得または設定します。 |
![]() | IsTransient | ツールボックス項目が遷移的かどうかを示す値を取得します。 |
![]() | Locked | ToolboxItem がロックされているかどうかを示す値を取得します。 |
![]() | Properties | プロパティのディクショナリを取得します。 |
![]() | TypeName | ツールボックス項目が呼び出されたときに作成する IComponent の型の完全限定名を取得または設定します。 |
![]() | Version | この ToolboxItem のバージョンを取得します。 |

名前 | 説明 | |
---|---|---|
![]() | CreateComponents | オーバーロードされます。 ツールボックス項目で作成するように設定されているコンポーネントを作成します。 |
![]() | Equals | オーバーロードされます。 オーバーライドされます。 2 つの ToolboxItem インスタンスが等しいかどうかを判断します。 |
![]() | GetHashCode | オーバーライドされます。 このインスタンスのハッシュ コードを返します。 |
![]() | GetType | オーバーロードされます。 指定した型のインスタンスを作成します。 |
![]() | Initialize | 作成する型を指定して、現在のツールボックス項目を初期化します。 |
![]() | Lock | ツールボックス項目をロックして、プロパティが変更されないようにします。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | オーバーライドされます。 現在の ToolboxItem を表す String を返します。 |

名前 | 説明 | |
---|---|---|
![]() | CheckUnlocked | ツールボックス項目が現在ロックされている場合は、例外がスローされます。 |
![]() | CreateComponentsCore | オーバーロードされます。 ツールボックス項目が呼び出されたときに、コンポーネントまたはコンポーネントの配列を作成します。 |
![]() | Deserialize | 指定したシリアル化情報オブジェクトから、このツールボックス項目の状態を読み込みます。 |
![]() | FilterPropertyValue | プロパティ値を返す前に、フィルタ処理をします。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | GetType | オーバーロードされます。 指定した型のインスタンスを作成します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
![]() | OnComponentsCreated | ComponentsCreated イベントを発生させます。 |
![]() | OnComponentsCreating | ComponentsCreating イベントを発生させます。 |
![]() | Serialize | 指定したシリアル化情報オブジェクトに、このツールボックス項目の状態を保存します。 |
![]() | ValidatePropertyType | オブジェクトが特定の型であるかどうかを検証します。 |
![]() | ValidatePropertyValue | プロパティ ディクショナリに割り当てる前にプロパティを検証します。 |


名前 | 説明 | |
---|---|---|
![]() | System.Runtime.Serialization.ISerializable.GetObjectData | このメンバの説明については、GetObjectData メソッドに関するトピックを参照してください。 |

- ToolboxItemのページへのリンク