ToolboxItemCreatorCallback イベントを処理するメソッドを表します。
<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] JScript では、.NET Framework のデリゲートを利用することができます。ただし、独自に定義することはできません。
パラメータ [Visual Basic, C#, C++]
コールバック メソッドの宣言のパラメータは、ToolboxItemCreatorCallback デリゲートの宣言と同じでなければなりません。
- serializedObject
ToolboxItem を作成するデータを含むオブジェクト。 - format
ToolboxItem を作成するクリップボード データ形式の名前。
解説
このデリゲートのメソッド シグネチャと同じメソッド シグネチャを持つツールボックス アイテム クリエータ用メソッドを実装し、そのメソッドを用いて、ツールボックスに置かれた特定のクリップボード データ形式の任意のオブジェクトから、ツールボックス 項目を作成することができます。たとえば、TextBox を作成するツールボックス アイテム クリエータ用メソッドをデザインして、クリップボードからツールボックスに貼り付けられたテキストを格納できます。 IToolboxService の AddCreator メソッドを使用して、特定のデータ型に対する ToolboxItemCreatorCallback イベント ハンドラをツールボックスに追加できます。 serializedObject パラメータは、データ オブジェクトを格納します。
ToolboxItemCreatorCallback デリゲートを作成する場合は、イベントを処理するメソッドを識別します。イベントをイベント ハンドラに関連付けるには、デリゲートのインスタンスをイベントに追加します。デリゲートを削除しない限り、そのイベントが発生すると常にイベント ハンドラが呼び出されます。イベント ハンドラ デリゲートの詳細については、「 イベントとデリゲート 」を参照してください。
使用例
[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 内)