Handling Events In C#

Here, I am going to demonstrate how to declare events in C# and how to call that event and so on.

An Event is a message sent by an object that indicates the occurrence of an action. The Action could be done by user interaction, such as button click or property change and so on.
The object that raises the event is called event sender. Event sender does not know which method or object is going to receive the event when it raises. 
The Event is declared by using the "Event" keyword.
  1. public class PrintKOT: AbstracTransaction  
  2.    {  
  4.        public event SaveDetailsHandler PrintHandlerEvent;  
  7.        public PrintKOT(TransactionProperties trnprt, ApiParameters parameters)  
  8.        {  
  9.            param = parameters;  
  10.            trn = trnprt;  
  11.            suport = new Transactions.Support(parameters);  
  12.        }  
A Delegate is a type that holds the reference of the method. Delegate is declared with a signature that shows return type and the parameters. Delegate acts like an intermediary between event code and the code that handles the event. If you want to pass specific parameters through the event, then you can use the delegate.
Delegate is declared by the "delegate" keyword.
  1. public class SaveDetailArgs : EventArgs  
  2.     {  
  3.         public List<ItemProperty> ItemList { getset; }  
  4.         public bool isReprint { getset; }  
  5.     }  
  7.     public delegate void SaveDetailsHandler(object sender, SaveDetailArgs args);  
The EventArgs class is the base class for all the event data classes. EventArgs is also the class you can use when your event does not have any data associated with it. When you create an event that is only meant to notify other classes that something happened and does not need to pass any data.
How to call an Event 
  • First, we need to check whether the event is raised or not.
  • If event is not null, then pass your parameter if needed. 
  • Here, PrintHandlerEvent doesn't know which method is going to receive this.
  1. public override bool btnClick(ReportTotalSummary reportsSum)  
  2.        {  
  3.            if (trn.saveDetailItemDetailsList.Count > 0)  
  4.            {  
  6.                    if (PrintHandlerEvent != null)  
  7.                    {  
  8.                        SaveDetailArgs args = new SaveDetailArgs() { ItemList = lst, isReprint = false };  
  9.                        PrintHandlerEvent.Invoke(nullargs) 
  11.                    }  
  13.            }  
  14.            return true;  
  15.        }  
After invoking the event, we need to define event handlers.
Event Handler 
To respond to the event, we need to define the event handler method in the event receiver class. This event handler should match with delegate signature. 
In event handler, you are going to perform the action. In my example, I need to print the report. 
  1. PrintKot kot = new PrintKot();  
  2. kot.PrintHandlerEvent += TransactionWindow_PrintHandlerEvent;  
  1. private void TransactionWindow_PrintHandlerEvent(object sender, SaveDetailArgs args)  
  2. {  
  3.     if (args.isReprint)  
  4.     {  
  5.         RePrintKOT_Report();  
  6.     }  
  7.     else  
  8.     {  
  9.         NewKOTPrint(args.ItemList);  
  10.     }  
  11.   }