Click here to Skip to main content
15,861,172 members
Articles / Web Development / ASP.NET
Article

Broadcasting Events through a Control Hierarchy

Rate me:
Please Sign up or sign in to vote.
4.69/5 (13 votes)
3 Mar 20065 min read 87K   814   47   21
Implement support for broadcasting events to all ancestors in a control hierarchy for Win Forms applications.

Introduction

When events occur in a Windows application, a set of registered handlers are invoked. Only the handlers that are directly registered with the control are invoked. However, there may be times when it is more desirable to have all the controls in a hierarchy handle a single event. For instance, suppose that a Panel has a set of buttons, all of which perform similar behavior. It might be desirable to have the Panel handle the Click event rather than having a separate handler for each button. This is not possible using native framework functionality.

This article discusses the EventBroadcastProvider which was created to solve the problem of relaying events through the control hierarchy. Using this class, a specific event can be monitored and relayed to all the handlers through the hierarchy.

Event broadcasting

Event broadcasting in the context of this article is not related to that which is supported by a MulticastDelegate, rather it refers to the ability for events raised in one control to be relayed up the control hierarchy to all ancestors, thereby giving them the ability to handle the event as well, all without coupling the child to its parent.

Consuming

One of the primary design constraints was for a simple system, something that can be consumed with little code or complications. Event broadcasting is handled by a single class, EventBroadcastProvider, which hooks the necessary events in all controls of the hierarchy (as discussed below). To add support for broadcasting, add the following class initialization to your code. Once initialized, all management of the events and relays are handled automatically. Out of sight, out of mind.

C#
EventBroadcastProvider broadcastEvent =
     EventBroadcastProvider.CreateProvider(this.panel1, "Click");

As shown, this hooks the control panel1 as well as all its children and relays the Click event when raised.

Hooking control events

No direct support exists for broadcasting event. An event handler can only be invoked by the object that raised the event. In the image below, button1 and panel1 both have a Click event handler, but when the user clicks button1, only that handler is invoked, not the handler for panel1 even though button1 is a child control of panel1:

Image 1

Likewise, if only panel1 has a handler, then no handler is invoked when button1 is clicked:

Image 2

To support broadcasting, all the events within the child controls call Relay, which is then responsible for relaying the events up the control hierarchy. There are two types of events that must be hooked, those that occur when the controls are added or removed, and those that are to be relayed. The reason for monitoring when the controls are added or removed is to enable the dynamically added controls to respond to the relayed events just as their static counterparts do. The other type of event is the one provided when EventBroadcastProvider is initialized. Because the CreateProvider method takes the event name as a string, that event must be dynamically located using reflection and the event handler associated at that time. The following code demonstrates how the events are hooked and how recursion is used to hook all the controls in the control hierarchy. A similar method removes the handlers when controls are removed from the hierarchy:

C#
private void HookPrimaryEvents(Control control)
{
    //
    // hook static events.
    //
    control.ControlAdded += m_controlAddedHandler;
    control.ControlRemoved += m_controlRemovedHandler;

    //
    // Use reflection to look for event that will be hooked.
    //
    Type typ = control.GetType();
    EventInfo theEvent = typ.GetEvent(m_eventName);
    theEvent.AddEventHandler(control, m_genericHandler);

        //
    // recursively hook events for nested controls
    //
    foreach (Control subControl in control.Controls)
    {
        HookPrimaryEvents(subControl);
    }
}

Relaying events

Relaying events ensures that when an event is raised on a control, all the ancestor controls also receive that event for handling. But not all ancestor controls need to handle the event.

The following code demonstrates the process of relaying events up the control hierarchy:

C#
protected void Relay(object sender, EventArgs ea)
{
    //
    // Make sure to end when the target control is the 
    // only one that send the event. This is because 
    // it is assumed that there is already
    // that event being handled directly.
    //
    if (object.ReferenceEquals(sender, m_control))
        return;

    //
    // Must flag object to prevent reentry.
    //
    if (m_noReentry) return;

    //
    // Locate the event method used to raise the event. 
    // Currently must be in the format On<EventName>(...).
    //
    string eventMethName =
        String.Format(EventMethodTemplate, m_eventName);

    //
    // Invoke the event passing the event arguments for 
    // each ancestor
    //
    m_noReentry = true;
    Control curControl = ((Control)sender).Parent;
    while (curControl != null)
    {
        MethodInfo mi =
            curControl.GetType().GetMethod(eventMethName, 
                                  BindingFlags.NonPublic |
                                  BindingFlags.Public |
                                  BindingFlags.FlattenHierarchy |
                                  BindingFlags.Instance);

        //
        // Ignore any classes that do not support the event.
        //
        if (mi != null)
        {
            mi.Invoke(curControl, new object[] { ea });
        }

        //
        // End loop it target control has been reached.
        //
        if (object.ReferenceEquals(curControl, m_control))
            break;

        //
        // Get the parent, or null if no parent exists.
        //
        curControl = curControl.Parent;
    }
    m_noReentry = false;
}

Reentry can be a problem for a common method. This is because when the Click event, for instance, is invoked it is also invoked for all ancestor controls. Therefore, as the event is relayed to ancestor controls, they also invoke the Relay method as a response to the event; however, the purpose of Relay is to walk the control hierarchy which already happened when the first control's event was raised. To prevent this, a flag indicates that the method is currently being processed and therefore should not allow reentry. Once the handler is done processing the other controls, the flag is cleared.

The primary work is handled in a loop that evaluates each control's parent. The standard convention for invoking events is through the On… method such as OnClick(…). The convention is for the method to contain the name of the event; therefore this method can be easily derived from the targeted event name. Once found, the method can be invoked thereby raising the event for the parent control. This process continues until Relay has reached the control specified when creating the EventBroadcastProvider instance.

Creating dynamic event handler

During the implementation of EventBroadcastProvider, it was determined, as an EventHandler cannot be down-casted, that a strongly-typed delegate must be created. This process is handled automatically and is transparent to the consumer.

The following diagram shows how this is handled. The only class the consumer has to worry about is the EventBroadcastProvider; however, when CreateProvider is called, the class is dynamically created using reflection. Therefore, EventBroadcastProvider can ask the derived class for the strongly-typed delegate through the RelayDelegate property, which is then hooked to the event:

Image 3

The following code is responsible for actually creating the derived class. Don't be intimidated by the amount of operations emitted. The only purpose of the derived class is to provide a strongly-typed event handler that, when called, will pass the responsibility back to the Relay method in EventBroadcastProvider (which is where the ancestor controls are visited).

[Editor comment: Line breaks used to avoid scrolling.]

C#
private static EventBroadcastProvider CreateHandlerForEvent(
                                                  EventInfo ei)
{
    EventBroadcastProvider result = null;

    string namespaceName = 
        typeof(EventBroadcastProvider).Namespace;
    string eventName = ei.Name;
    string className = eventName + "BroadcastProvider";

    AssemblyName assemblyName = new AssemblyName();
    assemblyName.Name = className + "Assembly";

    AppDomain appDomain = AppDomain.CurrentDomain;

    AssemblyBuilder assBuilder = 
        appDomain.DefineDynamicAssembly(assemblyName, 
                                  AssemblyBuilderAccess.Run);
    ModuleBuilder modBuilder =
        assBuilder.DefineDynamicModule(className + "Module");
    TypeBuilder typBuilder =
        modBuilder.DefineType(className, TypeAttributes.Public, 
                                typeof(EventBroadcastProvider));

    FieldBuilder fldBuilder =
        typBuilder.DefineField("m_handler", typeof(Delegate), 
                                     FieldAttributes.Private);

    ILGenerator ilGen = null;

    //
    // Build the RelayDelegate property.
    //
    PropertyBuilder relayDelegateBuilder = 
        typBuilder.DefineProperty("RelayDelegate", 
                                  PropertyAttributes.None, 
                                  typeof(Delegate), null);
    MethodBuilder get_relayDelegateBuilder = 
        typBuilder.DefineMethod("get_RelayDelegate", 
                                MethodAttributes.HideBySig|
                                MethodAttributes.Virtual|
                                MethodAttributes.Family|
                                MethodAttributes.SpecialName, 
                                typeof(Delegate), null);

    ilGen = get_relayDelegateBuilder.GetILGenerator();
    ilGen.DeclareLocal(typeof(Delegate));
    ilGen.Emit(OpCodes.Nop);
    ilGen.Emit(OpCodes.Ldarg_0);
    ilGen.Emit(OpCodes.Ldfld, fldBuilder);
    ilGen.Emit(OpCodes.Stloc_0);
    ilGen.Emit(OpCodes.Ldloc_0);
    ilGen.Emit(OpCodes.Ret);

    relayDelegateBuilder.SetGetMethod(get_relayDelegateBuilder);

    //
    // Build the HandleEvent method.
    //
    MethodBuilder handleEventBuilder =
        typBuilder.DefineMethod("HandleEvent", 
        MethodAttributes.Private | MethodAttributes.HideBySig, 
        null, 
        new Type[] { typeof(object), 
            ei.EventHandlerType.GetMethod("Invoke").
                          GetParameters()[1].ParameterType });
    ilGen = handleEventBuilder.GetILGenerator();

    ilGen.Emit(OpCodes.Nop);
    ilGen.Emit(OpCodes.Ldarg_0);
    ilGen.Emit(OpCodes.Ldarg_1);
    ilGen.Emit(OpCodes.Ldarg_2);
    ilGen.Emit(OpCodes.Call, 
        typeof(EventBroadcastProvider).GetMethod("Relay", 
        BindingFlags.Instance|BindingFlags.NonPublic));
    ilGen.Emit(OpCodes.Nop);
    ilGen.Emit(OpCodes.Ret);

    //
    // Build the constructor.
    //
    ConstructorBuilder ctorBuilder =
        typBuilder.DefineConstructor(
                    MethodAttributes.Public| 
                    MethodAttributes.HideBySig, 
                    CallingConventions.HasThis, null);

    ilGen = ctorBuilder.GetILGenerator();
    ilGen.Emit(OpCodes.Ldarg_0);
    ilGen.Emit(OpCodes.Ldnull);
    ilGen.Emit(OpCodes.Stfld, fldBuilder);
    ilGen.Emit(OpCodes.Ldarg_0);
    ilGen.Emit(OpCodes.Call, 
        typeof(EventBroadcastProvider).
        GetConstructor(BindingFlags.Instance|
                       BindingFlags.NonPublic, null, 
                       Type.EmptyTypes, null));
    ilGen.Emit(OpCodes.Nop);
    ilGen.Emit(OpCodes.Nop);
    ilGen.Emit(OpCodes.Ldarg_0);
    ilGen.Emit(OpCodes.Ldarg_0);
    ilGen.Emit(OpCodes.Ldftn, handleEventBuilder);
    ilGen.Emit(OpCodes.Newobj, 
        ei.EventHandlerType.GetConstructors()[0]);
    ilGen.Emit(OpCodes.Stfld, fldBuilder);
    ilGen.Emit(OpCodes.Nop);
    ilGen.Emit(OpCodes.Ret);

    result = (EventBroadcastProvider)Activator.
                CreateInstance(typBuilder.CreateType());

    return result;
}

Event bubbling

Event bubbling is a companion concept of event broadcasting. Web application developers are more comfortable with bubbling events because web applications support this concept, but Windows applications do not.

With small modifications, the EventBroadcastProvider can be converted into an EventBubbleProvider. This is currently being developed and a companion article will be written to demonstrate how to bubble Windows application events.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhi Pin
mr.dd11-Nov-12 9:52
mr.dd11-Nov-12 9:52 
QuestionReally good job ! Pin
okimi014-Mar-12 1:31
okimi014-Mar-12 1:31 
GeneralGreat stuff Pin
culianu11-Feb-11 3:13
culianu11-Feb-11 3:13 
GeneralExcellent Pin
JK Mathew6-Oct-10 1:35
JK Mathew6-Oct-10 1:35 
GeneralMy vote of 5 Pin
Naushervan30-Jun-10 23:37
Naushervan30-Jun-10 23:37 
QuestionLicense? Pin
RRGuthrie29-Apr-10 4:41
RRGuthrie29-Apr-10 4:41 
AnswerRe: License? Pin
Peter Rilling29-Apr-10 11:18
Peter Rilling29-Apr-10 11:18 
GeneralMy vote of 2 Pin
mehdi askeroglu11-Nov-09 5:45
mehdi askeroglu11-Nov-09 5:45 
GeneralComplete Solution Pin
MAHorn30-Oct-07 11:58
MAHorn30-Oct-07 11:58 
GeneralCrystal Reports for .NET - Format Event in Windows Form Pin
Pélège1-Oct-06 15:29
Pélège1-Oct-06 15:29 
GeneralCompact Framework Pin
spacetravelisboring28-Sep-06 7:49
spacetravelisboring28-Sep-06 7:49 
GeneralRe: Compact Framework Pin
Peter Rilling12-Mar-09 11:10
Peter Rilling12-Mar-09 11:10 
GeneralCode has errors.... Pin
geodan71-Sep-06 16:02
geodan71-Sep-06 16:02 
GeneralCode update... Pin
Peter Rilling7-Mar-06 19:49
Peter Rilling7-Mar-06 19:49 
GeneralUserControls Pin
andreboom7-Mar-06 9:26
andreboom7-Mar-06 9:26 
GeneralRe: UserControls Pin
Peter Rilling7-Mar-06 20:03
Peter Rilling7-Mar-06 20:03 
GeneralCode update... Pin
Peter Rilling3-Mar-06 16:09
Peter Rilling3-Mar-06 16:09 
QuestionHow to get invocation list using reflection? Pin
Peter Rilling26-Feb-06 17:49
Peter Rilling26-Feb-06 17:49 
AnswerRe: How to get invocation list using reflection? Pin
Peter Rilling27-Feb-06 19:11
Peter Rilling27-Feb-06 19:11 
GeneralRe: How to get invocation list using reflection? Pin
Marek.T6-Apr-06 11:39
Marek.T6-Apr-06 11:39 
GeneralRe: How to get invocation list using reflection? Pin
Peter Rilling6-Apr-06 21:53
Peter Rilling6-Apr-06 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.