IEventBindingService Interface

Definition

Provides a service for registering event handlers for component events.

C#
public interface IEventBindingService
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface IEventBindingService
Derived
Attributes

Examples

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.

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

Remarks

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.

Methods

CreateUniqueMethodName(IComponent, EventDescriptor)

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

GetCompatibleMethods(EventDescriptor)

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

GetEvent(PropertyDescriptor)

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

GetEventProperties(EventDescriptorCollection)

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

GetEventProperty(EventDescriptor)

Converts a single event descriptor to a property descriptor.

ShowCode()

Displays the user code for the designer.

ShowCode(IComponent, EventDescriptor)

Displays the user code for the specified event.

ShowCode(Int32)

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

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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

See also