IEventBindingService 介面

定義

提供為元件事件登錄事件處理常式的服務。

public interface class IEventBindingService
public interface IEventBindingService
[System.Runtime.InteropServices.ComVisible(true)]
public interface IEventBindingService
type IEventBindingService = interface
[<System.Runtime.InteropServices.ComVisible(true)>]
type IEventBindingService = interface
Public Interface IEventBindingService
衍生
屬性

範例

下列範例示範設計工具,此設計工具會在叫用設計工具新增的自定義快捷方式功能表命令時,使用 IEventBindingService 連結元件的事件與事件處理程式。 若要使用範例,請將它編譯為類別庫、從 Windows Forms 專案新增參考、將類別庫中的元件新增至 [工具箱],方法是以滑鼠右鍵按兩下 [工具箱] 並選取 [自定義工具箱],然後選擇類別庫,然後按兩下 [確定],然後將 EventControl 的實例新增至表單。 然後以滑鼠右鍵按下 EventControl,然後按兩下 [連接 testEvent 快捷方式] 選單命令。 系統會建立空的事件處理程式方法,並在 Form 的初始化程式代碼中,初始化 EventControl 的 testEvent 方法至這個事件處理程式。

#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;
using namespace System::Security::Permissions;

// 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 ref 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.
private:
   void ConnectEvent( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      IEventBindingService^ eventservice = dynamic_cast<IEventBindingService^>(this->Component->Site->GetService( System::ComponentModel::Design::IEventBindingService::typeid ));
      if ( eventservice != nullptr )
      {
         // Attempt to obtain a PropertyDescriptor for a 
         // component event named "testEvent".
         EventDescriptorCollection^ edc = TypeDescriptor::GetEvents( this->Component );
         if ( edc == nullptr || edc->Count == 0 )
                  return;
         EventDescriptor^ ed = nullptr;

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

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

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

public:
   property System::ComponentModel::Design::DesignerVerbCollection^ Verbs 
   {
      // Provides a designer verb command for the designer's 
      // shortcut menu.
      [PermissionSetAttribute(SecurityAction::Demand, Name="FullTrust")]
      virtual System::ComponentModel::Design::DesignerVerbCollection^ get() override
      {
         DesignerVerbCollection^ dvc = gcnew DesignerVerbCollection;
         dvc->Add( gcnew DesignerVerb( "Connect testEvent",gcnew EventHandler( this, &EventDesigner::ConnectEvent ) ) );
         return dvc;
      }
   }
};

// EventControl is associated with the EventDesigner and displays 
// instructions for demonstrating the service.

[Designer(EventDesigner::typeid)]
public ref class EventControl: public System::Windows::Forms::UserControl
{
public:
   event System::EventHandler^ testEvent;
   EventControl()
   {
      this->BackColor = Color::White;
      this->Size = System::Drawing::Size( 320, 96 );
   }

public:
   ~EventControl()
   {
   }

protected:
   virtual void OnPaint( System::Windows::Forms::PaintEventArgs^ e ) override
   {
      e->Graphics->DrawString( "IEventBindingService Example Control", gcnew System::Drawing::Font( FontFamily::GenericMonospace,10 ), gcnew SolidBrush( Color::Blue ), 5, 5 );
      e->Graphics->DrawString( "Use the \"Connect testEvent\" command of the", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 5, 22 );
      e->Graphics->DrawString( "right-click shortcut menu provided by this", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 5, 32 );
      e->Graphics->DrawString( "control's associated EventDesigner to create", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 5, 42 );
      e->Graphics->DrawString( "a new event handler linked with the testEvent", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 5, 52 );
      e->Graphics->DrawString( "of this control in the initialization code", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 5, 62 );
      e->Graphics->DrawString( "for this control.", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 5, 72 );
   }
};
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);
        }
    }
}
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.
    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    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.
        Private 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 (eventservice IsNot 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

備註

事件系結服務可讓您從設計工具程式代碼將事件處理程式與元件事件連結。

若要使用 IEventBindingService連結事件處理程式與元件事件,您必須先取得 EventDescriptor 您要連結之元件事件的 。 IEventBindingService提供方法,可將轉換成 EventDescriptorPropertyDescriptor ,您可以使用事件處理程式方法名稱來設定事件。

TypeDescriptor物件提供GetEvents方法,可讓您用來取得EventDescriptorCollection元件之每個事件的包含EventDescriptor物件。 針對每個傳遞至任一PropertyDescriptorEventDescriptor方法IEventBindingService的 ,傳GetEventProperty回 的和 GetEventProperties 方法。 從或 GetEventPropertiesGetEventProperty回的每個PropertyDescriptor都有字串的屬性類型。 您可以將這個字串設定為值,這個值表示事件處理程式方法的名稱,以使用 SetValuePropertyDescriptor方法連結事件。

方法

CreateUniqueMethodName(IComponent, EventDescriptor)

建立指定元件和事件之事件處理常式方法的唯一名稱。

GetCompatibleMethods(EventDescriptor)

取得具有與指定事件相容之方法簽章的事件處理常式方法的集合。

GetEvent(PropertyDescriptor)

取得指定屬性描述項表示之事件的 EventDescriptor (如果指定屬性描述項表示事件)。

GetEventProperties(EventDescriptorCollection)

將事件描述項集合轉換為屬性描述項集合。

GetEventProperty(EventDescriptor)

將單一事件描述項轉換為屬性描述項。

ShowCode()

顯示設計工具的使用者程式碼。

ShowCode(IComponent, EventDescriptor)

顯示指定事件的使用者程式碼。

ShowCode(Int32)

在指定行中顯示設計工具的使用者程式碼。

適用於

另請參閱