Events and Delegates

Predefined events

Events and delegates go hand in hand. Now we are considering Predefined events like

  • Click
  • Closed
  • Closing
  • DragDrop
  • Enter
  • Load
  • Leave etc

We normally use predefined events in our programming practice. Multiple events can share the same delegate type. The event is declared as the delegate type. In C# we must follow precise signature for Handler.

void OnClick(object o,EventArgs ea)
{
   //Code
}

Example of Events:

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  namespace Eventhandling
  {
   /// <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;
       private System.Windows.Forms.Button btnNewControl;
           
       public Form1()
       {
           //
           // Required for Windows Form Designer support
           //
           InitializeComponent();

           //
           // TODO: Add any constructor code after
                  InitializeComponent call
           //
       }

       /// <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()
       {
           this.btnNewControl = new  System.Windows.Forms.Button();
           this.SuspendLayout();
           //
           // btnNewControl
           //
           this.btnNewControl.Name = "btnNewControl";
           this.btnNewControl.Size = new System.Drawing.Size(112, 32);
           this.btnNewControl.TabIndex = 0;
           this.btnNewControl.Text = "New Control";
           this.btnNewControl.Click += new System.EventHandler(this.btnNewControl_Click);
           //
           // Form1
           //
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(304, 237);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.btnNewControl});
           this.Name = "Form1";
           this.Text = "Events";
           this.ResumeLayout(false);

       }
       #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 btnAdd(object o,EventArgs ea)
       {
           
           o_IntCounter++;
           MessageBox.Show(o_IntCounter.ToString());  

       }

       private void btnNewControl_Click(object sender, System.EventArgs e)
       {
           System.Int32 b_intCount;
           System.Int32 b_intXaxis=120;
           System.Int32 b_intYaxis=80;
           for(b_intCount=1;b_intCount<=3;b_intCount++,b_intYaxis+=20)
           {
               ///new buttons are created at run time
               ///with there location and names.
               Button b1=new Button();
               b1.Parent=this;
               b1.Location=new Point(b_intXaxis,b_intYaxis);
               b1.Name="Click1"+b_intCount;
               b1.Text="Click Me";
               b1.Click+=new EventHandler(btnAdd);  
           }
       }
   }
}


The above program creates three buttons at run time and when any of the buttons is clicked the Click event of that button is fired.

for(b_intCount=1;b_intCount<=3;b_intCount++,b_intYaxis+=20)
{
   ///new buttons are created at run time
   ///with there location and names.
   Button b1=new Button();
   b1.Parent=this;
   b1.Location=new Point(b_intXaxis,b_intYaxis);
   b1.Name="Click1"+b_intCount;
   b1.Text="Click Me";
   b1.Click+=new EventHandler(btnAdd);  
}

The Click event belongs to the button class. We will reference it when we are registering a delegate.

b1.Click+=new EventHandler(btnAdd);

The delegate EventHandler is also defined in the System namespace of Dot net Framework library. All what we have to do is to define our callback function that is invoked when the button is clicked and accepts two arguments object and EventArgs type conforms the signature of delegate EventHandler defined in System namespace

public void btnAdd(object o,EventArgs ea)
{
  o_IntCounter++;
  MessageBox.Show(o_IntCounter.ToString());  
}

The handler btnAdd() Method catches this event and shows a message box that indicates a number that how many times these buttons are clicked. The above example is quite self explanatory so just copy the code in a new project and run for yourself.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Memory is like an orgasm. It's a lot better if you don't have to fake it.” - Seymour Cray