Library tutorials & articles

Events and Delegates

Customized events

Don’t confuse these terms because I have categorized events so that the explanation become simple. Now what do we mean by customize events, they are events which we define according to our needs and are not defined. For e.g we want to raise an event whenever a dynamic control is created on the form. To declare an event, first define the delegate for that event, if none is already declared. The delegate type defines the set of argument that are to be passed to the method which will act as event handler.

Example of Customized Events:

namespace Eventhandling
  {
    public delegate void IamClicked();

   /// <summary>
   /// Summary description for Form1.
   /// </summary>
   public class Form1 : System.Windows.Forms.Form
   {
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ComponentModel.Container components = null;
       public System.Int32 o_IntCounter=0;
         public event IamClicked ClickMe;
       System.Int32 o_intXaxis=120;
       System.Int32 o_intYaxis=80;
   

       public Form1()
       {
           //
           // Required for Windows Form Designer support
           //
           InitializeComponent();
           //
           // TODO: Add any constructor code after      
                    InitializeComponent call
           //
           Button b1=new Button();
           b1.Parent=this;
           b1.Location=new Point(o_intXaxis,o_intYaxis);
           b1.Name="Click1";
           b1.Text="Click Me";
                 ClickMe+=new IamClicked(Show);
           ClickMe();
       }

       /// <summary>
       /// Clean up any resources being used.
       /// </summary>
       protected override void Dispose( bool disposing )
       {
           if( disposing )
           {
               if (components != null)
               {
                   components.Dispose();
               }
           }
           base.Dispose( disposing );
       }

       #region Windows Form Designer generated code
       /// <summary>
       /// Required method for Designer support - do not modify
       /// the contents of this method with the code editor.
       /// </summary>
       private void InitializeComponent()
       {
           //
           // Form1
           //
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(304, 237);
           this.Name = "Form1";
           this.Text = "Events";

       }
       #endregion

       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main()
       {
           Application.Run(new Form1());
       }
      /// <summary>
      /// Event Handler Function Which is called when the
      /// Click Event is Raised.
      /// </summary>
      /// <param name="o"></param>
      /// <param name="ea"></param>
       public void Show()
       {        
           MessageBox.Show("JUST BORN");  
       }
   }
}

The above program shows hoe we can fire our own events. In this program a button is created dynamically.

Button b1=new Button();
b1.Parent=this;
b1.Location=new Point(o_intXaxis,o_intYaxis);
b1.Name="Click1";
b1.Text="Click Me";
     ClickMe+=new IamClicked(Show);
ClickMe();

The delegate and event defined  in above program are

public delegate void IamClicked();
public event IamClicked ClickMe;

The delegate points to the following function.

public void Show()
{
   MessageBox.Show("JUST BORN");  
}

When the ClickME event is fired the delegate attached to this event call the above function Show. Look at the signature of the function and the delegate both are same. The function just shows a message box with the message “Just Born”.  

Comments

  1. 17 Oct 2006 at 10:34
    Good article for beginners.

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Events and Delegates.

Leave a comment

Sign in or Join us (it's free).

Faisal Jawaid
AddThis

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Want to stay in touch with what's going on? Follow us on twitter!