Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating Custom User Controls : Basic � III

0.00/5 (No votes)
18 Oct 2007 1  
Creating Custom User Controls : Basic � III

Introduction

Describing Indexers, Events and Delegates.

Background

Creating Custom User Controls : Basic - I

Creating Custom User Controls : Basic - II

Indexers

Let's say that you have a control, or data class that has a collection of items. Instead of exposing
the collection by making it public through a property, it can be easier to add an indexer to the
control. An indexer is a special type of property that makes the class be indexed as an array, so
that its objects can be retrieved based on an index value.

For example, if an object called list has an indexer, you could get its collection items by
reading list[1], list[2], and so on. Without an indexer, you would need to access its items as
list.Items[1], list.Items[2], and so on. The difference between indexers and properties is that
indexer accessors take parameters.

The default declaration of an indexer is:
public this[int index]
{
get
{
//return the object at the index;
}
set
{
//set the object at the index;
}
}

Usually, the indexer returns an object held in a member array at the given index.

Generally speaking you can use indexers when you can structure your data into an array; this
means that the data must be numerable. For example, you will use a property to store the
temperature in degrees, but you will use an indexer to store the day of the week.

Events and Delegates

Events and delegates are at the heart of programming for the Windows platform, because they are
the main mechanisms by which the application interacts with the user (but they can be used for
other purposes as well). Events and delegates allow a control (or any type of class, for that matter),
to send a signal when something happens to it, and you can write a C# method to be executed
automatically in response to that signal. The signal is the event itself, which is said to be fired. The
delegate is a type of object that allows you to register the local C# method (which we'll call the
event handler) to be executed when the event fires.

To explain with an example, let's say we have a form and a button on that form. We want to know
when the button is clicked, so we can react to that action by executing some C# code. This link
between the button and the form is done through an event. When the button is clicked, the Click
event of the button is fired to notify that the button has been clicked. To react to the Click event,
you create an event handler, which is a C# method that is executed when the Click event fires.

Events define actions that occur in a control. These actions can be caused by users interacting with
the control, or by other implemented logic. A mouse click is an event, a key press is also an event
fired by the control to notify that certain action has occurred in it. An event is fired by a sender
and is captured by a receiver. A special type called a delegate does the link between the sender and
the receiver.

When an event happens in a control, that control knows how to execute a method of the class using that
control; in order for this to work, a reference to the method to be executed must be sent to the
control. The delegate is such a method reference. A delegate is a data type that defines a method
template, and a delegate instance is a reference to a particular method. Each event that can happen
in a control knows how to execute a certain delegate type, which supports the parameters the event
needs to send to its parent (these parameters contain the details of the event, which differ
depending on the event).

Let's see how delegates are declared:
public delegate ( "parameter list" );

Example

public delegate int myDelegate( int intValue );

This delegate represents the template for a method that takes as parameter an integer value, and
also returns an integer. Multiple methods in other classes can be wired to this event if they match
the event's delegate template. These methods are then called when the event is fired. Usually event
delegates have the following layout:

public delegate void "myEventHandler"(object sender, "EventArgs" e);

Here, "myEventHandler" is the local method that executes when the event fires.

Now let's see how events are defined. An event must be declared in the control that fires it, and the
default declaration looks like this:

public event "myEventHandler" "Event_name";

Example:
public event ClickEventHandler Click;

The receiver methods that are called when the event is fired are called event handlers. In other
words, when creating a control that needs to notify the form or other controls when a certain action
has happened, you need to add a public event to the control, and fire it when the action occurs.
Then, any class that holds a reference to your control can subscribe to get notified by associating
an event handler to the event. Let's see now how we can fire an event and how to handle it in
another class.

Firing an event is usually done from a protected virtual method of the control that fires the event,
declared like the one given below:

protected virtual void On"event name"(EventArgs e)
{
if("event name" != null)
{
"event name"(this,e);
}
}

The firing method is virtual, which means that you can override it when deriving from the control,
if you need to change its default behavior.

Summary

Indexers
Events and Delegates

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