c# 事件,vs2010的样例
来源:互联网 发布:怎么租淘宝店 编辑:程序博客网 时间:2024/06/06 09:56
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();
}
}
- c# 事件,vs2010的样例
- vs2010 c#的异常处理
- 皮肤控件IrisSkin4.dll调用样例-vs2010-c#
- C#的事件机制
- C#的事件机制
- c#事件的四步曲
- C# 事件的继承
- C# 事件的继承
- C#的事件机制
- C#的事件机制
- C#的事件机制
- C#事件的学习体会
- C# 事件的继承
- c#事件的定义
- c#的事件
- C#的事件机制。
- C# 事件的继承
- C#的事件机制
- Joomla模板下载网站阅兵式
- 线程基础知识
- sof和pof配置文件
- VC实现线程池
- C# 修改IIS相关设置代码
- c# 事件,vs2010的样例
- volatile
- C#判断字符串中是否有中文
- SQL2000/2005字符串拆分为列表通用函数
- 免安装版的Tomcat设置开机自启动
- 84827046
- 调试
- Linux内核源码阅读之内存映射篇
- 学习型组织的特点