Naz-Tek Services, Inc.

Writing Software To Provide Strategic Advantage

Home     Development Guidelines     Design Patterns     Knowledgebase     Trainingbase     Blog Feed     Events     Tools     About Us     Contact Us      
Memory Management     Event Handling     Multi-Threading     Serialization      

Create custom events

Step 1: declare the delegate object, in the namespace not inside a class

namespace NazTek.Sample.EventHandling

{

public delegate void MyEventHandler(object sender, MyEventArgs e);

}

Step 2: define an EventArgs subclass.  It is only useful if you want to pass data through the event otherwise you may simply use System.EventArgs class provided by the CLR

public class MyEventArgs : System.EventArgs

{

              private string _arg;

              public string Message { get { return _arg; } }

        // note: you may pass any type through this subclass, including

             // custom classes and structs.  Type used here is string

              public MyEventArgs(string arg) { _arg = arg; }

}

Step 3: define the event publisher class.  This is the class that contains the event to publish and the method that raises the event

public class MyEventPublisher

{

    public MyEventPublisher() { }

    // declare the event to publish

    public event MyEventHandler MyEvent;

    // define a method to raise the event

    public void RaiseMyEvent() { OnMyEvent(this, new MyEventArgs("my event message")); }

    // define a method to encapsulate the actual raising of the event - best practice

    private void OnMyEvent(object sender, MyEventArgs e)

    {

        // raise the event if there's at least one subscriber

        if (MyEvent != null)

            MyEvent(sender, e);

    }

}

Step 4: Define the event subscriber class.  This is the class that contains a reference to the publisher class , explicitly attaches to the public event defined in that class, and defines the handler method

public class MyEventConsumer

{

    // get a reference to the publisher and define the method to attach to the custom event.

    public MyEventConsumer(MyEventPublisher publisher)

    {

        // attach to the custom event

        publisher.MyEvent += new MyEventHandler(publisher_MyEvent);

    }

    // define the method to handle the custom event

    void publisher_MyEvent(object sender, MyEventArgs e)

    {

        System.Console.WriteLine("Message from my event: " + e.Message);

    }

}

Finally, create a console application to test the event handling process

class Program

{

    static void Main(string[] args)

    {

        MyEventPublisher myPublisher = new MyEventPublisher();

        MyEventConsumer myConsumer = new MyEventConsumer(myPublisher);

    myPublisher.RaiseMyEvent();

        System.Console.ReadLine();

    }

}

 
Bubble events

ASP.NET server controls, custom controls, and WebForms all inherit from the Control class (Figure 1) which has a virtual (overridable in VB) method called OnBubbleEvent.  You may override this method on a parent control as shown in the code

public partial class _Default : System.Web.UI.Page

{

    protected override bool OnBubbleEvent(object source, EventArgs args)

    {

        // handle events from children controls here

        Response.Write(source.ToString());

        // prevent this event from further propagation

        const bool CANCEL_FURTHER_BUBBLE = true;

        return CANCEL_FURTHER_BUBBLE;

    }

}

If you do so on a parent control, events from the children will bubble up to this overridden method and you will have to handle the children’s events on this parent method, unless they are cancelled on the children, which you may also do as shown in the code

public partial class WebUserControl : System.Web.UI.UserControl

{

    protected override bool OnBubbleEvent(object source, EventArgs args)

    {

        // handle specific child event here

        Response.Write(source.ToString());

        // prevent this event from further propagation

        const bool CANCEL_BUBBLE = true;

        return CANCEL_BUBBLE;

    }

}

Finally, you may also bubble a child event sequentially without waiting for the CLR to do it on your behalf, as shown in the code

protected override bool OnBubbleEvent(object source, EventArgs args)

{

    // bubble the child event sequentially

    RaiseBubbleEvent(source, args);

    // handle other stuff here

    Response.Write(source.ToString());

    // prevent this event from further propagation

    const bool CANCEL_BUBBLE = true;

    return CANCEL_BUBBLE;

}