using System; using System.Collections; namespace WindowsApplication5 { /// <summary> /// Summary description for NameList. /// </summary> public class NameList { ArrayList list; public event NameListEventHandler nameListEvent; public NameList() { // // TODO: Add constructor logic here // list = new ArrayList(); } public void Add(string name) { list.Add(name); if(nameListEvent != null) { nameListEvent(this, new NameListEventArgs(name, list.Count)); } } } } using System; public delegate void NameListEventHandler(object source, WindowsApplication5.NameListEventArgs args); namespace WindowsApplication5 { /// <summary> /// Summary description for NameListEventArgs. /// </summary> public class NameListEventArgs : EventArgs { string name; int count; public NameListEventArgs() { // // TODO: Add constructor logic here // } public NameListEventArgs(string name, int count) { this.name = name; this.count = count; } public string Name { get { return name; } } public int Count { get { return count; } } } } private void button1_Click(object sender, System.EventArgs e) { NameList names =new NameList(); names.nameListEvent += new NameListEventHandler(NewName); names.nameListEvent += new NameListEventHandler(CurrentCount); names.Add("MSDN"); names.Add("Webcast"); } public static void NewName(object source, NameListEventArgs args) { Console.WriteLine(args.Name + "was added to the list"); } public static void CurrentCount(object source, NameListEventArgs args) { Console.WriteLine("list currently has " + args.Count + " items."); }
posted on 2010-05-13 22:15 java课程设计 阅读(261) 评论(0) 收藏 举报