IEventBindingService Interface
Provides a service for registering event handlers for component events.
Assembly: System (in System.dll)
The event binding service provides a way to link an event handler with a component event from designer code.
To link an event handler with a component event using the IEventBindingService, you must first obtain an EventDescriptor for the event of the component you intend to link. The IEventBindingService provides methods that can convert an EventDescriptor to a PropertyDescriptor which you can use to configure the event with an event handler method name.
The TypeDescriptor object provides a GetEvents method that you can use to obtain an EventDescriptorCollection containing EventDescriptor objects for each event of a component. The GetEventProperty and GetEventProperties methods of the IEventBindingService return a PropertyDescriptor for each EventDescriptor passed to either method. Each PropertyDescriptor returned from GetEventProperty or GetEventProperties has a property type of string. You can set this string to a value that indicates the name of the event-handler method to link the event with using the SetValue method of the PropertyDescriptor.
The following example demonstrates a designer that uses the IEventBindingService to link a component's event with an event handler at design time when the custom shortcut menu command that the designer adds for the component is invoked. To use the example, compile it to a class library, add a reference from a Windows Forms project, add the component in the class library to the Toolbox by right-clicking the Toolbox and selecting Customize Toolbox..., then choosing the class library and clicking OK, and add an instance of the EventControl to a Form. Then right-click the EventControl and click the Connect testEvent shortcut menu command. An empty event handler method is created and the EventControl's testEvent method is initialized to this event handler in the initialization code for the Form.
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. <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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.