英語で読む

次の方法で共有


IEventBindingService インターフェイス

定義

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

C#
public interface IEventBindingService
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface IEventBindingService
派生
属性

次の例は、 を使用 IEventBindingService して、デザイナーがコンポーネントに追加するカスタム ショートカット メニュー コマンドが呼び出されたときに、デザイン時にコンポーネントのイベントをイベント ハンドラーにリンクするデザイナーを示しています。 この例を使用するには、クラス ライブラリにコンパイルし、Windows フォーム プロジェクトから参照を追加し、ツールボックスを右クリックして [ツールボックスのカスタマイズ] を選択し、クラス ライブラリを選択して [OK] をクリックし、EventControl のインスタンスを Form に追加して、クラス ライブラリ内のコンポーネントをツールボックスに追加します。 次に、EventControl を右クリックし、[テストの接続][イベント] ショートカット メニュー コマンドをクリックします。 空のイベント ハンドラー メソッドが作成され、EventControl の testEvent メソッドが Form の初期化コードでこのイベント ハンドラーに初期化されます。

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.
        private 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);
        }
    }
}

注釈

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

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

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

メソッド

CreateUniqueMethodName(IComponent, EventDescriptor)

指定したコンポーネントおよびイベントのイベント ハンドラー メソッドに対して一意の名前を作成します。

GetCompatibleMethods(EventDescriptor)

指定したイベントと互換性のあるメソッド シグネチャを持つイベント ハンドラー メソッドのコレクションを取得します。

GetEvent(PropertyDescriptor)

指定したプロパティ記述子がイベントを表す場合、そのイベントの EventDescriptor を取得します。

GetEventProperties(EventDescriptorCollection)

一連のイベント記述子を一連のプロパティ記述子に変換します。

GetEventProperty(EventDescriptor)

単一のイベント記述子をプロパティ記述子に変換します。

ShowCode()

デザイナーに関連するコードを表示します。

ShowCode(IComponent, EventDescriptor)

指定したイベントに関連するコードを表示します。

ShowCode(Int32)

デザイナーに関連するコードの指定した行を表示します。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください