Meta

Custom events

VB does not have a syntax for defining events that allows you to specify the field, the add method or the remove method. Those are always implicitly generated.

Now, most of the time this doesn’t really matter. Most of the time, the code you write in the add and remove method is going to be the same boilerplate code over and over and over again, so you’re going to want to just let the compiler do its thing and not worry about it too much. However, there are some situations in which you might want to take over managing an event’s delegate. The most common case that I know of is the situation in which you have an object that raises a lot of events. For example, a Form can raise something like 85 different events. If you accept the default compiler behavior, this means that the compiler will generate a field for each and every event to store the event handlers for that event. Which means in the case of Form, that it would generate something like 85 fields, even though in most cases programmers only ever handle about 4-5 events on a Form!

One alternative to wasting all that space is to use a hashtable to store delegates for just the events that someone is handling. To do this, though, you need to be able to control what happens when someone hooks up to or unhooks from an event. So, in VB 2005, we’re introducing something we call custom events that look something like this:

Custom events are declared with the Custom modified on the event declaration and have to explicitly state their delegate type. Custom events have three parts: an AddHandler method that is called when someone is hooking up to the event, a RemoveHandler method that is called when someone unhooks from the event and a RaiseEvent method that is called when the class does a RaiseEvent on the event. The AddHandler and RemoveHandler methods take a delegate of the type of the event. The RaiseEvent method takes the same parameters as the event delegate does. So, to store all event delegates in one hashtable, you could do the following:

One thing to notice that’s different from C# is that we make you specify a RaiseEvent method. This is to enable the RaiseEvent statement, which C# doesn’t have, to work properly. Otherwise, it works pretty much the same way C#’s event declaration does.

Oh, I mean that in your code we can use RaiseEvent to raise the custom event. However, in previous version of Visual Basic I allways create an ‘OnEventName’ virtual procedure in which I put the ‘RaiseEvent’ statement and call this procedure when I want to fire it. So we can remain this pattern in the new custom event. I mean let the ‘RaiseEvent’ accessor do the same task just like original event statement, and then use the OnEventName procedure to raise the event. I want to know whether your pattern have other advantages so that I can use for reference.

In v1, C# uses the System.ComponentModel.EventHandlerList class to store the delegates for the handled events. A large part of the BCL uses this class, particularly anything derived from System.ComponentModel.Component, which is the base class for every Windows Forms control, and anything derived from System.Web.UI.Control, which is the base class for all ASP.NET controls.

Since all of these cases provide access to the list via the protected Events property, what advantage would there be in declaring a separate private instance of Dictionary(Of String, EventHandler) to store the delegates? Even if your class doesn’t derive from one of these base classes, it would make more sense to keep a consistent approach to your events.

I’m trying to create custom event in a custom control and will later convert it as an anthem-ised custom control to use that. Actually my requirement is such a control which populates itself according to the data stored in database. e.g. there are 5 Product groups then the control has some property (like “NumberOfProducts” ) which then populates itself as a product groups having 5 products i.e inside render event of that control i’m creating a table with 5 <td> tags containing a single table in each of those <td>, having 3 rows for ProductName, productImage and ProductDescription. When I click any of those Product tables say product groups it will then populate its corresponding Product Sub-groups according to its ProductGroupID in a similar way as of product group and on clicking on any of those product subgroups the same control will populate the product according to the selected product SubGroupId. To do this I created some properties. i.e ProductDepth, ProductID but need a custom event as OnProductClick for my control so that I will get some space to do my work with in that event. I have created that event but it’s not firing that event .

==========

Control code

==========

// Defines the Click event.

public event EventHandler ProductClick;

// Invokes delegates registered with the ProductClick event.

protected virtual void OnProductClick(EventArgs e)

{

if (ProductClick != null)

{

ProductClick(this, e);

}

}

// Method of IPostBackEventHandler that raises change events.

public void RaisePostBackEvent(string eventArgument)

{

this.OnProductClick(EventArgs.Empty);

}

I’m creating that control instance dynamically on pageclass level(not in page load event)

MyControl objProductFrm = new MyControl();

And on prostback, inside a button click event I’m populating the control and attaching the event with that control as below