C# 2.0学习之--事件2
2006-06-23 18:41
323 查看
//Copyright (C) Microsoft Corporation. All rights reserved.
// events2.cs
using System;
namespace MyCollections
{
using System.Collections;
// 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 EventHandler 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 EventHandler(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 EventHandler(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();
}
}
}
// events2.cs
using System;
namespace MyCollections
{
using System.Collections;
// 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 EventHandler 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 EventHandler(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 EventHandler(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();
}
}
}
csc events2.cs events2
相关文章推荐
- C# 2.0学习之--事件1
- C# 2.0学习之--事件1
- C# 2.0学习之--事件2
- C#学习笔记之三:C#接口与事件委托
- C#学习:事件
- 精通C#学习笔记之事件(一)使用委托的弊端
- [读书笔记]C#学习笔记四: C#2.0泛型 可控类型 匿名方法和迭代器
- c#2.0泛型学习(一)
- C#基础学习系列之:委托和事件
- C#中的委托和事件学习(续)
- (原创)c#学习笔记08--面向对象编程简介02--OOP技术06--事件
- [读书笔记]C#学习笔记二: 委托和事件的用法及不同.
- C#学习基本概念之事件(四)---EventHandler委托(事件处理)
- c#事件学习总结
- c# 事件学习
- C#事件学习
- C#2.0锐利体验之杂项技术及未来发展学习笔记
- C# 2.0学习之--集合1
- C# 2.0学习之--代理1
- C# 2.0学习之--Yield