C# .NET - what are events - Asked By aman on 17-Sep-11 01:35 AM

hi all,

what are events?
what problem does events solves?
why we use events?

please give example so that i can understand it better manner..../....../....../

thanks
Aman Khan
Reena Jain replied to aman on 17-Sep-11 01:40 AM
hi,

what are Events  :

The Event model in C# finds its roots in the event programming model that is popular in asynchronous programming. The basic foundation behind this programming model is the idea of "publisher and subscribers." In this model, you have publishers who will do some logic and publish an "event." Publishers will then send out their event only to subscribers who have subscribed to receive the specific event.
protected void btSomeButton_Click (object sender, EventArgs ea){
   Button btnSomeButton = sender as Button;
   btnSomeButton.Text = "I was clicked!";
}

The sender is the control that the action is for (say OnClick, it's the button).

The EventArgs are arguments that the implementor of this event may find useful. With OnClick it contains nothing good, but in some events, like say in a GridView 'SelectedIndexChanged', it will contain the new index, or some other useful data.

Riley K replied to aman on 17-Sep-11 02:52 AM
A C# event is a class member that is activated whenever the event it was designed for occurs

Anyone interested in the event can register and be notified as soon as the event fires. At the time an event fires, registered methods will be invoked.


Here is an example of an event

class Eventdemo : Form
{
  // custom event
  public event Startdelegate StartEvent;
 
  public Eventdemo()
  {
    Button clickMe = new Button();
 
    clickMe.Parent = this;
    clickMe.Text = "Click Me";
    clickMe.Location = new Point(
      (ClientSize.Width - clickMe.Width) /2,
      (ClientSize.Height - clickMe.Height)/2);
 
    // an EventHandler delegate is assigned
    // to the button's Click event
    clickMe.Click += new EventHandler(OnClickMeClicked);
 
    // our custom "Startdelegate" delegate is assigned
    // to our custom "StartEvent" event.
    StartEvent += new Startdelegate(OnStartEvent);
 
    // fire our custom event
    StartEvent();
  }
 
  // this method is called when the "clickMe" button is pressed
  public void OnClickMeClicked(object sender, EventArgs ea)
  {
    MessageBox.Show("You Clicked My Button!");
  }
 
  // this method is called when the "StartEvent" Event is fired
  public void OnStartEvent()
  {
    MessageBox.Show("I Just Started !");
  }
 
  static void Main(string[] args)
  {
    Application.Run(new Eventdemo());
  }
}

you can refer this link from MSDN

http://msdn.microsoft.com/en-us/library/17sde2xt(v=vs.71).aspx

Regards
smr replied to aman on 17-Sep-11 09:54 AM
hi

An event is a placeholder for code that is executed when the event is triggered, or fired. Events are fired by a user action, program code, or by the system.

The event method, or event procedure, formally consists of the procedure name followed by two arguments. The first argument, or parameter, is the object firing the event; the second argument is of the type System.EventArgs.

Example;

The following simple example shows a class, ListWithChangedEvent, which is similar to the standard ArrayList class, but also invokes a Changed event whenever the contents of the list change. Such a general-purpose class could be used in numerous ways in a large program.

// events1.cs
using System;
namespace MyCollections
{
   using System.Collections;
 
   // A delegate type for hooking up change notifications.
   public delegate void ChangedEventHandler(object sender, EventArgs e);
 
   // A class that works just like ArrayList, but sends event
   // notifications whenever the list changes.
   public class ListWithChangedEvent: ArrayList
   {
    // An event that clients can use to be notified whenever the
    // elements of the list change.
    public event ChangedEventHandler Changed;
 
    // Invoke the Changed event; called whenever list changes
    protected virtual void OnChanged(EventArgs e)
    {
     if (Changed != null)
      Changed(this, e);
    }
 
    // Override some of the methods that can change the list;
    // invoke event after each
    public override int Add(object value)
    {
     int i = base.Add(value);
     OnChanged(EventArgs.Empty);
     return i;
    }
 
    public override void Clear()
    {
     base.Clear();
     OnChanged(EventArgs.Empty);
    }
 
    public override object this[int index]
    {
     set
     {
      base[index] = value;
      OnChanged(EventArgs.Empty);
     }
    }
   }
}
 
namespace TestEvents
{
   using MyCollections;
 
   class EventListener
   {
    private ListWithChangedEvent List;
 
    public EventListener(ListWithChangedEvent list)
    {
     List = list;
     // Add "ListChanged" to the Changed event on "List".
     List.Changed += new ChangedEventHandler(ListChanged);
    }
 
    // This will be called whenever the list changes.
    private void ListChanged(object sender, EventArgs e)
    {
     Console.WriteLine("This is called when the event fires.");
    }
 
    public void Detach()
    {
     // Detach the event and delete the list
     List.Changed -= new ChangedEventHandler(ListChanged);
     List = null;
    }
   }
 
   class Test
   {
    // Test the ListWithChangedEvent class.
    public static void Main()
    {
    // Create a new list.
    ListWithChangedEvent list = new ListWithChangedEvent();
 
    // Create a class that listens to the list's change event.
    EventListener listener = new EventListener(list);
 
    // Add and remove items from the list.
    list.Add("item 1");
    list.Clear();
    listener.Detach();
    }
   }
}
Output
 
This is called when the event fires.
This is called when the event fires.

refer
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
http://www.techrepublic.com/article/working-with-events-in-c/1050284