Home > DeveloperSection > Blogs > Custom Events and Event Argument in C#

Custom Events and Event Argument in C#


C# C# 
Ratings:
0 Comment(s)
 3132  View(s)
Rate this:

Custom Events and Event Argument in C#


In this blog I am trying to explain the concept of Custom events and Event argument in c#.

Events

An event in C# is a way by which any class can provide notifications to clients of that class when some activities are happens to an object. We have thousand of event occurs in real day to day life and we respond those appropriately knowingly or unknowingly. But in term computer and programming each event is basically an indication to do something as bounded with that event. The most common and familiar use for events are button click, keypress, select from list etc.


Events, however, need not be used only for graphical interfaces. Events provide a generally useful way for objects to signal state changes that may be useful to clients of that object. Events are an important building block for creating classes that can be reused in a large number of different programs.


Events are declared using delegates (provide delegate link). If you have not yet studied the Delegates Tutorial, you should do so before continuing. Recall that a delegate object encapsulates a method so that it can be called anonymously. An event is a way for a class to allow clients to give it delegates to methods that should be called when the event occurs. When the event occurs, the delegate(s) given to it by its clients are invoked.


Technically, an event is a member of a type, just like properties and methods, except its type is a delegate rather than a class or structure. A delegate, or at least an instance of a delegate, is an object that contains a reference to a method. In the case of an event, the method the delegate refers to is the event handler. As an example, the Button class has a Click event defined as type EventHandler. The EventHandler delegate is defined like so:

public event PelindromEventHander pelindromStringEvent;

 Using Custom Event Argument:


So far we use the build in event argument (Like EventArg) for particular event handler.  For example on button click, we have following event argument which is passed to its event handler.

private void btnOK_Click(object sender, EventArgs e)

Now we define our own custom event argument and passed it to custom event’s user defined event handler.

Brief description of demo example


In this demo we have a custom Event argument class as “PelindromEventArgs” which inherit “EventArgs” system Event argument class. And this custom argument class contain an” EventMessage” property, which contain error message of that event argument.

 

On button click we check the inputted string is palindrome or not. If the sting inputted by user is palindrome then we generate an event that display the message what you want.

Now the following code illustrates this.

PropertyChangedEventArgs.cs

using System;

namespace CustomEventHandlerDemo

{

    public class PelindromEventArgs:EventArgs

    {

        private string eventMessagetext;

        public string EventMessage

        {

            get{return eventMessagetext;}

        }

        public PelindromEventArgs(string msg)

        {

            this.eventMessagetext=msg;

        }

    }

}

PropertyChangedEventArgs.cs

using System;

using System.Windows.Forms;

using CustomEventHandlerDemo;

public delegate void PelindromEventHander(object sender, PelindromEventArgs e);

namespace CustomEventHandlerDemo

{

    public partial class Form1 : Form

    {

        public event PelindromEventHander pelindromStringEvent;

        public Form1()

        {

            InitializeComponent();

            this.pelindromStringEvent += new PelindromEventHander(On_pelindromString);

        }

        private void btnOK_Click(object sender, EventArgs e)

        {

            char[] charray = txtInput.Text.Trim().ToCharArray();

            Array.Reverse(charray);

            string revStr = new string(charray);

            if (revStr.Equals(txtInput.Text.Trim()))

            {

     PelindromEventArgs evnt = new PelindromEventArgs("String pelindrom Event occurs!");

                pelindromStringEvent(this, evnt);

            }

            txtInput.Text = string.Empty;

        }

        public void On_pelindromString(object s, PelindromEventArgs e)

        {

            if (e != null)

                MessageBox.Show(e.EventMessage);

        }

    }

}

 Finally build and run this program will generate following desire output.


 I hope this article had been greatly conceptualize the concept of Custom Events in c#.

 Thanks you.

 


Don't want to miss updates? Please click the below button!

Follow MindStick