Google
 

Friday, June 01, 2007

How to use EventHandler in C#

  1. The EventHandler will be instantiated when there are methods having registered to it
  2. To raise the event please first check if the EventHandler is null or not
Sample:


using System;
using System.Collections.Generic;
using System.Text;
using COW.Business;

namespace COW.Hybridization
{
public interface IShortcutItem
{
event EventHandler Saving;

event EventHandler Saved;

event EventHandler Canceled;

string Content
{
get;
set;
}

int ParentID
{
get;
}

string ParentName
{
get;
}

string ItemName
{
get;
}

string ContentName
{
get;
}

void Save();

void Cancel();

bool IsValidContent(out string errorMessage);
}

public abstract class AbstractShortcutItem : IShortcutItem
{
protected string content = string.Empty;
protected int parentID = int.MaxValue;
protected string parentName = string.Empty;
protected string contentName = string.Empty;
protected string itemName = string.Empty;

public AbstractShortcutItem(
int parentID,
string parentName,
string itemName,
string contentName)
{
if (itemName == null || itemName == string.Empty
|| contentName == null || contentName == string.Empty)
throw new ArgumentNullException();

this.parentID = parentID;
this.parentName = parentName;
this.itemName = itemName;
this.contentName = contentName;
}

public abstract event EventHandler Saving;

public abstract event EventHandler Saved;

public abstract event EventHandler Canceled;

public string Content
{
get{
return content;
}
set{
content
= value;
}
}

public int ParentID
{
get { return parentID; }
}

public string ParentName
{
get { return parentName; }
}

public string ItemName
{
get { return itemName; }
}

public string ContentName
{
get { return contentName; }
}

public abstract void Save();

public abstract void Cancel();

public virtual bool IsValidContent(out string errorMessage)
{
errorMessage
= string.Empty;
if (content == null || content.Trim() == string.Empty){
errorMessage
= string.Format(Properties.Resources.PleaseInput,contentName);
return false;
}
return true;
}

}

// town
public class TownShortcutItem : AbstractShortcutItem
{
public TownShortcutItem():base(int.MaxValue,
string.Empty,
Properties.Resources.Town,
Properties.Resources.ObjectName)
{
}

public override void Save()
{
string message ;
if (!IsValidContent(out message))
throw new ArgumentException("Invalid town content!");

if(Saving != null)
Saving(
this, new EventArgs());

Town town
= new Town();
town.Name
= content;
Town.Add(town);

if(Saved != null)
Saved(
this, new EventArgs());
}

public override void Cancel()
{
// nothing
if(Canceled != null)
Canceled(
this, new EventArgs() );
}

public override bool IsValidContent(out string errorMessage)
{
if (!base.IsValidContent(out errorMessage))
return false;

if (Town.IsDuplicate(content)){
errorMessage
= Properties.Resources.NameBeingUsed;
return false;
}
return true;
}

public override event EventHandler Saving ;

public override event EventHandler Saved;

public override event EventHandler Canceled ;
}
// village
public class VillageShortcutItem : AbstractShortcutItem
{
public VillageShortcutItem(int parentID)
:
base(parentID,
Town.Get(parentID).Name,
Properties.Resources.Village,
Properties.Resources.ObjectName)
{
if (parentID == int.MaxValue)
throw new ArgumentException("Invalid parent ID!");
}

public override void Save()
{
string message;
if (!IsValidContent(out message))
throw new ArgumentException("Invalid village content!");

if(Saving != null)
Saving(
this, new EventArgs());

Village village
= new Village();
village.TownID
= parentID;
village.Name
= content;
Village.Add(village);

if(Saved != null)
Saved(
this, new EventArgs());
}

public override void Cancel()
{
if(Canceled != null)
Canceled(
this, new EventArgs());
}

public override bool IsValidContent(out string errorMessage)
{
if (!base.IsValidContent(out errorMessage))
return false;

if (Village.IsDuplicate(content, parentID)){
errorMessage
= Properties.Resources.NameBeingUsed;
return false;
}

return true;
}

public override event EventHandler Saving;

public override event EventHandler Saved;

public override event EventHandler Canceled;
}
// group
public class GroupShortcutItem : AbstractShortcutItem
{
public GroupShortcutItem(int parentID)
:
base(parentID,Village.Get(parentID).Name, Properties.Resources.Group, Properties.Resources.ObjectName)
{
if (parentID == int.MaxValue)
throw new ArgumentException("Invalid parent ID!");
}

public override void Save()
{
string message ;
if (!IsValidContent(out message))
throw new ArgumentException("Invalid group content!");

if(Saving != null)
Saving(
this, new EventArgs());

Group group
= new Group();
group.VillageID
= parentID;
group.Name
= content;
Group.Add(group);

if(Saved != null)
Saved(
this, new EventArgs());
}

public override void Cancel()
{
if(Canceled != null)
Canceled(
this, new EventArgs());
}

public override bool IsValidContent(out string errorMessage)
{
if (!base.IsValidContent(out errorMessage))
return false;

if (Group.IsDuplicate(content, parentID)){
errorMessage
= Properties.Resources.NameBeingUsed;
return false;
}

return true;
}

public override event EventHandler Saving;

public override event EventHandler Saved;

public override event EventHandler Canceled;
}
// cultivationhousehold
public class CultivationholdShortcutItem : AbstractShortcutItem
{
public CultivationholdShortcutItem(int parentID)
:
base(parentID, Group.Get(parentID).Name, Properties.Resources.CultivationHousehold, Properties.Resources.CultivationHouseholdName)
{
if (parentID == int.MaxValue)
throw new ArgumentException("Invalid parent ID!");
}

public override void Save()
{
string message;
if (!IsValidContent(out message))
throw new ArgumentException("Invalid cultivationhold name!");

if(Saving != null)
Saving(
this, new EventArgs());

CultivationHousehold household
= new CultivationHousehold();
household.GroupID
= parentID;
household.Name
= content;
CultivationHousehold.Add(household);

if(Saved != null)
Saved(
this, new EventArgs());
}

public override void Cancel()
{
if(Canceled != null)
Canceled(
this, new EventArgs());
}

public override bool IsValidContent(out string errorMessage)
{
if (!base.IsValidContent(out errorMessage))
return false;

if (CultivationHousehold.IsDuplicate(content, parentID)){
errorMessage
= Properties.Resources.NameBeingUsed;
return false;
}

return true;
}

public override event EventHandler Saving;

public override event EventHandler Saved;

public override event EventHandler Canceled;
}
// cow
public class CowShortcutItem : AbstractShortcutItem
{
public CowShortcutItem(int parentID)
:
base(parentID, CultivationHousehold.Get(parentID).Name, Properties.Resources.Cow, Properties.Resources.ObjectName)
{
if (parentID == int.MaxValue)
throw new ArgumentException("Invalid parent ID!");
}

public override void Save()
{
string message;
if (!IsValidContent(out message))
throw new ArgumentException("Invalid cow content!");

if(Saving != null)
Saving(
this, new EventArgs());

Cow cow
= new Cow();
cow.CultivationHouseholdID
= parentID;
cow.CowEarMark
= content;
Cow.Add(cow);

if(Saved != null)
Saved(
this, new EventArgs());
}

public override void Cancel()
{
if(Canceled != null)
Canceled(
this, new EventArgs());
}

public override bool IsValidContent(out string errorMessage)
{
if (!base.IsValidContent(out errorMessage))
return false;

if (Cow.IsDuplicate(Content)){
errorMessage
= Properties.Resources.CowEarMarkBeingUsed;
return false;
}

return true;
}
public override event EventHandler Saving;

public override event EventHandler Saved;

public override event EventHandler Canceled;
}
}



Sample to use the event:

private void ShortcutAddVillageLabel_Click(object sender, EventArgs e)
{
errorProvider.Clear();

if (SelectedTownID == INVALID_ID)
{
errorProvider.SetError(comboBoxTown, Properties.Resources.PleaseSelectTown);
return;
}
VillageShortcutItem village
= new VillageShortcutItem(SelectedTownID);
village.Saved
+= delegate {
BeginInvoke(
new RefreshObject(RefreshVillage));
};
using (ShortcutAdding shortcut = new ShortcutAdding(village))
{
shortcut.ShowDialog();
}
}

No comments: