次の方法で共有


IEventBindingService インターフェイス

コンポーネントのイベントに対するイベント ハンドラを登録するためのサービスを提供します。

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

<ComVisible(True)>
Public Interface IEventBindingService
[C#]
[ComVisible(true)]
public interface IEventBindingService
[C++]
[ComVisible(true)]
public __gc __interface IEventBindingService
[JScript]
public
   ComVisible(true)
interface IEventBindingService

解説

イベント バインディング サービスは、デザイナのコードから、イベント ハンドラをコンポーネント イベントとリンクする手段を提供します。

IEventBindingService を使用してイベント ハンドラをコンポーネント イベントにリンクするには、最初に、リンクするコンポーネントのイベントに対応する EventDescriptor を取得する必要があります。 IEventBindingService は、 EventDescriptorPropertyDescriptor に変換できるメソッドを提供します。これを使用して、イベント ハンドラのメソッド名でイベントを構成できます。

TypeDescriptor オブジェクトは GetEvents メソッドを提供します。このメソッドを使用すると、コンポーネントの各イベントに対する EventDescriptor を格納した EventDescriptorCollection を取得できます。 IEventBindingServiceGetEventProperty メソッドと GetEventProperties メソッドは、それぞれのメソッドに渡された EventDescriptor に対する PropertyDescriptor を返します。 GetEventProperty または GetEventProperties から返される各 PropertyDescriptor には、文字列型のプロパティがあります。この文字列には、 PropertyDescriptorSetValue メソッドを使用して、イベントをリンクするイベント処理メソッドの名前を示す値を設定できます。

使用例

[Visual Basic, C#, C++] IEventBindingService を使用して、デザイナがコンポーネントに対して追加したカスタム ショートカット メニュー コマンドが起動されたときに、デザイン時にコンポーネントのイベントをイベント ハンドラにリンクするデザイナの例を次に示します。この例を使用するには、コンパイルしてクラス ライブラリを作成し、Windows フォームのプロジェクトから参照を追加して、クラス ライブラリ内のコンポーネントをツールボックスに追加します。これを行うには、ツールボックスを右クリックして [アイテムの追加と削除] をクリックし、クラス ライブラリを選択して、[OK] をクリックし、EventControl のインスタンスをフォームに追加します。次に、EventControl を右クリックして、ショートカット メニュー コマンド [Connect testEvent] をクリックします。空のイベント ハンドラ メソッドが作成され、EventControl の testEvent メソッドが、フォームの初期化コードでこのイベント ハンドラに初期化されます。

 
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

Namespace EventDesignerTest
    ' This designer provides a "Connect testEvent" designer verb shortcut 
    ' menu command. When invoked, the command attaches a new event-handler 
    ' method named "testEventHandler" to the "testEvent" event of an 
    ' associated control.
    ' If a "testEvent" event of the associated control does not exist, 
    ' the IEventBindingService declares it.
    Public Class EventDesigner
        Inherits System.Windows.Forms.Design.ControlDesigner

        Public Sub New()
        End Sub

        ' When the "Connect testEvent" designer verb shortcut menu 
        ' command is invoked, this method uses the 
        ' IEventBindingService to attach an event handler to a "textEvent" event of the associated control.
        Public Sub ConnectEvent(ByVal sender As Object, ByVal e As EventArgs)
            Dim eventservice As IEventBindingService = CType(Me.Component.Site.GetService(GetType(System.ComponentModel.Design.IEventBindingService)), IEventBindingService)
            If Not (eventservice Is Nothing) Then
                ' Attempt to obtain a PropertyDescriptor for a 
                ' component event named "testEvent".
                Dim edc As EventDescriptorCollection = TypeDescriptor.GetEvents(Me.Component)
                If edc Is Nothing Or edc.Count = 0 Then
                    Return
                End If
                Dim ed As EventDescriptor = Nothing
                ' Search for an event named "testEvent".
                Dim edi As EventDescriptor
                For Each edi In edc
                    If edi.Name = "testEvent" Then
                        ed = edi
                        Exit For
                    End If
                Next edi
                If ed Is Nothing Then
                    Return
                End If
                ' Use the IEventBindingService to get a 
                ' PropertyDescriptor for the event.
                Dim pd As PropertyDescriptor = eventservice.GetEventProperty(ed)
                If pd Is Nothing Then
                    Return
                End If
                ' Set the value of the event to "testEventHandler".
                pd.SetValue(Me.Component, "testEventHandler")
            End If
        End Sub

        ' Provides a designer verb command for the designer's 
        ' shortcut menu.
        Public Overrides ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection
            Get
                Dim dvc As New DesignerVerbCollection()
                dvc.Add(New DesignerVerb("Connect testEvent", New EventHandler(AddressOf ConnectEvent)))
                Return dvc
            End Get
        End Property
    End Class

    ' EventControl is associated with the EventDesigner and displays 
    ' instructions for demonstrating the service.
    <Designer(GetType(EventDesigner))> _
    Public Class EventControl
        Inherits System.Windows.Forms.UserControl
        Public Event testEvent As System.EventHandler

        Public Sub New()
            Me.BackColor = Color.White
            Me.Size = New Size(320, 96)
        End Sub

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            MyBase.Dispose(disposing)
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            e.Graphics.DrawString("IEventBindingService Example Control", New Font(FontFamily.GenericMonospace, 10), New SolidBrush(Color.Blue), 5, 5)
            e.Graphics.DrawString("Use the ""Connect testEvent"" command of the", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 5, 22)
            e.Graphics.DrawString("right-click shortcut menu provided by this", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 5, 32)
            e.Graphics.DrawString("control's associated EventDesigner to create", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 5, 42)
            e.Graphics.DrawString("a new event handler linked with the testEvent", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 5, 52)
            e.Graphics.DrawString("of this control in the initialization code", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 5, 62)
            e.Graphics.DrawString("for this control.", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 5, 72)
        End Sub

    End Class
End Namespace

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

namespace EventDesignerTest
{
    // This designer provides a "Connect testEvent" designer verb shortcut 
    // menu command. When invoked, the command attaches a new event-handler 
    // method named "testEventHandler" to the "testEvent" event of an 
    // associated control.
    // If a "testEvent" event of the associated control does not exist, 
    // the IEventBindingService declares it.
    public class EventDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        public EventDesigner()
        {
        }
        
        // When the "Connect testEvent" designer verb shortcut menu 
        // command is invoked, this method uses the 
        // IEventBindingService to attach an event handler to a 
        // "textEvent" event of the associated control.
        public void ConnectEvent(object sender, EventArgs e)
        {
            IEventBindingService eventservice = (IEventBindingService)this.Component.Site.GetService(typeof(System.ComponentModel.Design.IEventBindingService));
            if( eventservice != null )
            {
                // Attempt to obtain a PropertyDescriptor for a 
                // component event named "testEvent".
                EventDescriptorCollection edc = TypeDescriptor.GetEvents(this.Component);                
                if( edc == null || edc.Count == 0 )
                    return;
                                
                EventDescriptor ed = null;
                // Search for an event named "testEvent".
                foreach(EventDescriptor edi in edc)
                    if(edi.Name == "testEvent")
                    {
                        ed = edi;
                        break;
                    }
                if( ed == null )
                    return;

                // Use the IEventBindingService to get a 
                // PropertyDescriptor for the event.
                PropertyDescriptor pd = eventservice.GetEventProperty(ed);
                if( pd == null )
                    return;                
                
                // Set the value of the event to "testEventHandler".
                pd.SetValue(this.Component, "testEventHandler");
            }
        }

                // Provides a designer verb command for the designer's 
                // shortcut menu.
        public override System.ComponentModel.Design.DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection dvc = new DesignerVerbCollection();
                dvc.Add(new DesignerVerb("Connect testEvent", new EventHandler(ConnectEvent)));
                return dvc;
            }
        }
    }

    // EventControl is associated with the EventDesigner and displays 
    // instructions for demonstrating the service.
    [Designer(typeof(EventDesigner))]
    public class EventControl : System.Windows.Forms.UserControl
    {
        public event System.EventHandler testEvent;

        public EventControl()
        {        
            this.BackColor = Color.White;    
            this.Size = new Size(320, 96);
        }

        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawString("IEventBindingService Example Control", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Blue), 5, 5);
            
            e.Graphics.DrawString("Use the \"Connect testEvent\" command of the", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 22);
            e.Graphics.DrawString("right-click shortcut menu provided by this", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 32);
            e.Graphics.DrawString("control's associated EventDesigner to create", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 42);
            e.Graphics.DrawString("a new event handler linked with the testEvent", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 52);
            e.Graphics.DrawString("of this control in the initialization code", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 62);
            e.Graphics.DrawString("for this control.", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 72);
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.Windows.Forms.dll>
#using <System.Data.dll>
#using <System.Drawing.dll>
#using <System.Design.dll>
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Drawing;
using namespace System::Data;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;

namespace EventDesignerTest
{
    // This designer provides a "Connect testEvent" designer verb shortcut 
    // menu command. When invoked, the command attaches a new event-handler 
    // method named "testEventHandler" to the "testEvent" event of an 
    // associated control.
    // If a "testEvent" event of the associated control does not exist, 
    // the IEventBindingService declares it.
public __gc class EventDesigner : public System::Windows::Forms::Design::ControlDesigner
{
public:
    EventDesigner()
    {
    }

    // When the "Connect testEvent" designer verb shortcut menu 
    // command is invoked, this method uses the 
    // IEventBindingService to attach an event handler to a 
    // "textEvent" event of the associated control.
    void ConnectEvent(Object* /*sender*/, EventArgs* /*e*/)
    {
        IEventBindingService* eventservice = dynamic_cast<IEventBindingService*>(this->Component->Site->GetService(__typeof(System::ComponentModel::Design::IEventBindingService)));
        if( eventservice != 0 )
        {
            // Attempt to obtain a PropertyDescriptor for a 
            // component event named "testEvent".
            EventDescriptorCollection* edc = TypeDescriptor::GetEvents(this->Component);                
            if( edc == 0 || edc->Count == 0 )
                return;

            EventDescriptor* ed = 0;
            // Search for an event named "testEvent".
            IEnumerator* myEnum = edc->GetEnumerator();
            while (myEnum->MoveNext())
            {
                EventDescriptor* edi = __try_cast<EventDescriptor*>(myEnum->Current);
                if(edi->Name->Equals(S"testEvent"))
                {
                    ed = edi;
                    break;
                }
            }
            if( ed == 0 )
                return;

            // Use the IEventBindingService to get a 
            // PropertyDescriptor for the event.
            PropertyDescriptor* pd = eventservice->GetEventProperty(ed);
            if( pd == 0 )
                return;                

            // Set the value of the event to "testEventHandler".
            pd->SetValue(this->Component, S"testEventHandler");
        }
    }

    // Provides a designer verb command for the designer's 
    // shortcut menu.
    __property System::ComponentModel::Design::DesignerVerbCollection* get_Verbs()
    {
        DesignerVerbCollection* dvc = new DesignerVerbCollection();
        dvc->Add(new DesignerVerb(S"Connect testEvent", new EventHandler(this, &EventDesigner::ConnectEvent)));
        return dvc;
    }

};

// EventControl is associated with the EventDesigner and displays 
// instructions for demonstrating the service.
[Designer(__typeof(EventDesigner))]
public __gc class EventControl : public System::Windows::Forms::UserControl
{
public:
    __event System::EventHandler* testEvent;

    EventControl()
    {        
        this->BackColor = Color::White;    
        this->Size = System::Drawing::Size(320, 96);
    }

protected:
    void Dispose( bool disposing )
    {
        UserControl::Dispose( disposing );
    }

    void OnPaint(System::Windows::Forms::PaintEventArgs* e)
    {
        e->Graphics->DrawString(S"IEventBindingService Example Control", new System::Drawing::Font(FontFamily::GenericMonospace, 10), new SolidBrush(Color::Blue), 5, 5);

        e->Graphics->DrawString(S"Use the \"Connect testEvent\" command of the", new System::Drawing::Font(FontFamily::GenericMonospace, 8), new SolidBrush(Color::Black), 5, 22);
        e->Graphics->DrawString(S"right-click shortcut menu provided by this", new System::Drawing::Font(FontFamily::GenericMonospace, 8), new SolidBrush(Color::Black), 5, 32);
        e->Graphics->DrawString(S"control's associated EventDesigner to create", new System::Drawing::Font(FontFamily::GenericMonospace, 8), new SolidBrush(Color::Black), 5, 42);
        e->Graphics->DrawString(S"a new event handler linked with the testEvent", new System::Drawing::Font(FontFamily::GenericMonospace, 8), new SolidBrush(Color::Black), 5, 52);
        e->Graphics->DrawString(S"of this control in the initialization code", new System::Drawing::Font(FontFamily::GenericMonospace, 8), new SolidBrush(Color::Black), 5, 62);
        e->Graphics->DrawString(S"for this control.", new System::Drawing::Font(FontFamily::GenericMonospace, 8), new SolidBrush(Color::Black), 5, 72);
    }
};
}

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

必要条件

名前空間: System.ComponentModel.Design

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

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

参照

IEventBindingService メンバ | System.ComponentModel.Design 名前空間 | PropertyDescriptor | EventDescriptor | PropertyDescriptorCollection | EventDescriptorCollection