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 !




Creating and invoking custom events


Posted Date: 07 Mar 2007    Resource Type: Code Snippets    Category: Event Handling

Posted By: Manikandan Balakrishnan       Member Level: Gold
Rating:     Points: 10



Events are used to handle the situations dynamically. Let say almost in all the windows application we are using button control, and the click event of it. Actually the click is happening in the button control, but user [we] able to handle the click event in form. The same way we can use the events for our own controls, business objects etc.

Example:
Class “Store” is used to add and delete the items. Also the class has the event called “OnDelete”, which is used to handle something before the delete operation. The user of the “Store” class can write his codes required to execute before the delete operation.

In this example I have handled the OnDelete event to show the warning message while deleting.



// Declare a delegate for the event handler.
public delegate void StoreEventHandler(string itemName,ref bool action);

public class Store
{
// Declare an event handler
public event StoreEventHandler OnDelete;

private Dictionary ItemList = new Dictionary();

public void Add(int Id,string Name)
{
ItemList.Add(Id,Name);
}

public void Delete(int Id)
{
bool canDelete=false;

//fire the on delete event[s]
OnDelete(ItemList[Id], ref canDelete);

if (canDelete)
{
ItemList.Remove(Id);
MessageBox.Show("Item Deleted sucessfully");
}
else
{
MessageBox.Show("Deletion canceld by the user");
}
}
}


How to use the Store object in your application:


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Store myStore = new Store();
myStore.OnDelete += new StoreEventHandler(myStore_OnDelete);

myStore.Add(1, "Milk");
myStore.Add(2, "Biscuts");

myStore.Delete(2);
}

void myStore_OnDelete(string itemName, ref bool action)
{
DialogResult r = MessageBox.Show("Are you sure you want to delete item '"+itemName+"' from the store","Confirm Item Delete", MessageBoxButtons.YesNo);
if (r == DialogResult.Yes)
{
action = true;
}
else
{
action = false;
}
}
}




Responses

Author: mani    08 May 2008Member Level: Silver   Points : 2
good it is nice


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.
Previous Resource: Handling events & delegates with code example
Return to Discussion Resource Index
Post New Resource
Category: Event Handling


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use