Events In C#

 

By Sanju

From:http://www.csharphelp.com/archives/archive253.html

An event is defined in C# as 'a member that enables an object or class to provide notifications'. Now, let us see in detail what an event is and how notifications are provided in C#. In an application, the change in status (or certain properties) of an object may result in some actions to be performed. For e.g., consider a dialog box with some buttons in it. Pressing any of these buttons will have to perform an action, for e.g., starting a new process or closing the dialog. This action will be defined by the user who is developing the application. Other than this, when the button is pressed, certain actions should be performed at the system level. These include changing the appearance of the button to give the user a feeling that the button is being pressed and so on. In common programming languages, this is done by placing the code that should be executed when button click occurs in some functions and invoking these functions when the user clicks the button. Here, clicking the button is normally referred to as an 'Event' and all the associated functions that are called as a result of an event as 'Event Handlers'. When an event occurs, all the event handlers are 'notified' (this simply means that all the event handler functions are invoked). To invoke all these functions when an event is triggered, the event should know what all functions are associated with it. We can now look at how events are implemented in C# and how it is associated with event handlers.

Let us start with an example. Consider that we are designing a Button control that can be used by other developers while developing screens. The Button can be placed in a dialog (or screen) and will have some properties associated with it. When a user clicks the button, some actions are to be performed that will be defined only at the time of coding the screen. Assume that this piece of code (i.e., the operations that should be performed when any of the buttons are clicked) will be put in a function named 'onButtonClick' by the user. The function will look like

  

Here, we have two parameters for the function. The first parameter is an object that identifies the source of event. A screen (or dialog) may contain more than one button and this parameter is used to identify which button is clicked by the user (the actions to be performed by each button will be different). The second parameter is an integer that indicates whether the action is a single click or double click (that may be of little use in the current context). The first step for implementing the event in button class is to declare a delegate for representing the event handler function (onButtonClick). The delegate declaration will be as follows:

public delegate void ButtonEventHandler(object source, int clickCount)

where ButtonEventHandler is the delegate name.(For those who do not have an idea about delegates- Delegates can be considered as 'function pointers' which is used to represent or invoke functions. The delegate type should be same as the corresponding function it represents, i.e., the parameters and return type of delegates and the corresponding function should be identical.). The delegate type ButtonEventHandler represents the function type that can be associated with the click event, i.e., any function that accepts two parameters (object and int) and returning void, can be associated with the click event. Now we can proceed with the event declaration. Let the event name be 'ButtonClick'. The event declaration will be as follows:

public event ButtonEventHandler ButtonClick;

The event declaration should have the keyword 'event' followed by the delegate type. The next step is associating the event handler function (onButtonAction) with the event (ButtonClick). This is to be done because the ButtonClick event should know about the functions that are to be executed when the event is triggered. The operator += is used to achieve this as shown in the following code:

b.ButtonClick += new ButtonEventHandler(onButtonAction);

Here, b is an instance of the class Button. (Also, note that the onButtonAction should have the same parameter and return types as ButtonEventHandler delegate.)

Now consider that the event is triggered. This can be done by the user or another process. In our case, the event is triggered when a user clicks on any of the buttons. On doing this, all the associated functions (onButtonAction in the current context) of ButtonClick event should be executed. This can be performed using the following code:

ButtonClick(this, count)

The parameters of ButtonClick should match with the delegate type (ButtonEventHandler). The following code consolidates all our discussion till now:

 

Before going to a sample program, we will discuss some more details regarding the event operations. The operator '-=' will remove the corresponding function from the Event, opposite to the behaviour of '+='. The following code

ButtonClick = null

will remove all the event handler functions from ButtonClick. Also, if you want to check whether any event handlers (or functions) are attached to the event, the code

if (ButtonClick != null)
can be used.

Finally, a sample program is given which adds and removes more than one event handler:

 
The output of the program will be as follows:

Inside Clicked !!!
Inside Event Handler !!!

Inside Clicked !!!
Inside Event Handler !!!
Inside Event Handler !!!

Inside Clicked !!!
Inside Event Handler !!!

Inside Clicked !!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值