IEventBindingService Interface


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides a service for registering event handlers for component events.

Namespace:   System.ComponentModel.Design
Assembly:  System (in System.dll)

public interface IEventBindingService

System_CAPS_pubmethodCreateUniqueMethodName(IComponent, EventDescriptor)

Creates a unique name for an event-handler method for the specified component and event.


Gets a collection of event-handler methods that have a method signature compatible with the specified event.


Gets an EventDescriptor for the event that the specified property descriptor represents, if it represents an event.


Converts a set of event descriptors to a set of property descriptors.


Converts a single event descriptor to a property descriptor.


Displays the user code for the designer.

System_CAPS_pubmethodShowCode(IComponent, EventDescriptor)

Displays the user code for the specified event.


Displays the user code for the designer at the specified line.

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.

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.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
    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 )

                EventDescriptor ed = null;
                // Search for an event named "testEvent".
                foreach(EventDescriptor edi in edc)
                    if(edi.Name == "testEvent")
                        ed = edi;
                if( ed == null )

                // Use the IEventBindingService to get a 
                // PropertyDescriptor for the event.
                PropertyDescriptor pd = eventservice.GetEventProperty(ed);
                if( pd == null )

                // 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
                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.
    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);

.NET Framework
Available since 1.1
Return to top