C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Handling events & delegates with code example


Posted Date: 02 Jul 2007    Resource Type: Code Snippets    Category: Event Handling

Posted By: daya       Member Level: Gold
Rating:     Points: 10



This article will demonstrate how to handle them at runtime with an example.


 
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace DelegateEventDemo
{

public delegate void AddEvent(object sender,EventArgs e);

class DelegateHandler
{

public event AddEvent OnAdditionToList;

public ArrayList al = new ArrayList();

public DelegateHandler()
{
// Registering the Changed method to event
OnAdditionToList += new AddEvent(Changed);
}

public void OnListChanged(EventArgs e)
{
// Publish the event : Listening
if (OnAdditionToList != null)
OnAdditionToList(this, e);
}

private void Changed(object sender,EventArgs e)
{
// Notify the user
Console.WriteLine("New element has been inserted into the list.");
}


public ArrayList Add(int num)
{
al.Add(num);
// Fier the event it on occure
OnListChanged(EventArgs.Empty);
return al;
}

public ArrayList Get()
{
return al;
}

}



class Program
{
static void Main(string[] args)
{
DelegateHandler dh = new DelegateHandler();

// Client side handling of event
dh.OnAdditionToList += new AddEvent(ClientFunction);

for (int i = 1; i <= 10; i++)
dh.Add(i * 10);

ArrayList al = dh.Get();

for (int i = 0; i < al.Count; i++)
Console.WriteLine(al[i].ToString());

Console.ReadLine();
}


public static void ClientFunction(object sender, EventArgs e)
{
Console.WriteLine("Client handler is called.");


}


}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Creating and invoking custom events
Previous Resource: Multi-Cast delegates
Return to Discussion Resource Index
Post New Resource
Category: Event Handling


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

masks masks masks

Contact Us    Privacy Policy    Terms Of Use