Code
/// <summary>
/// 事件的编写过程:
/// 1、事件参数类,以 .EventArgs为后缀。
/// 2、delegate委托,包括object源数据以及事件参数类e
/// 3、event事件,以委托为类型的事件。
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ss.Adding += new Students.AddingEventHandler(ss_Adding);
}
void ss_Adding(object sender, Students.AddingCancelEventArgs e)
{
if (e.Name.StartsWith("a") || ss.Contains(e.Name))//禁止a开头的名字或已有名字
e.Cancel = true;
if (!e.Cancel)
listBox1.Items.Add(e.Name);
else
{
MessageBox.Show("禁止以小写a开头的单词或名称已重复……");
}
}
Students ss = new Students();
private void button1_Click(object sender, EventArgs e)
{
if (((Button)sender).Text == "Add")
{
int age = 0;
if (textBox2.Text != string.Empty) age = textBox2.Text.ToInteger();
if (textBox1.Text != string.Empty)
ss.Add(new Student() { Name = textBox1.Text, Age = age });
}
else
{
MessageBox.Show(ss.Count.ToString());
}
}
}
public static class Extension//扩展方法
{
public static int ToInteger(this string temp)
{
return int.Parse(temp);
}
}
public class Students :CollectionBase
{
public delegate void AddingEventHandler(object sender, AddingCancelEventArgs e);//2、
public event AddingEventHandler Adding;//3、
public class AddingCancelEventArgs : CancelEventArgs//1、
{
public string Name { get; set; }
public AddingCancelEventArgs(bool xancel) : base(xancel) { }
public AddingCancelEventArgs(){ }
}
public void Add(Student temp)
{
bool cancel = false;
if (Adding != null)
{
AddingCancelEventArgs ace = new AddingCancelEventArgs() { Name = temp.Name };
Adding(List, ace);
cancel = ace.Cancel;
}
if (!cancel)
List.Add(temp);
}
public void Remove(Student temp)
{
List.Remove(temp);
}
public Student this[int index]
{
get { return (Student)List[index]; }
set { List[index] = value; }
}
public bool Contains(string name)
{
foreach (Student st in List)
{
if (st.Name == name) return true;
}
return false;
}
}
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
/// <summary>
/// 事件的编写过程:
/// 1、事件参数类,以 .EventArgs为后缀。
/// 2、delegate委托,包括object源数据以及事件参数类e
/// 3、event事件,以委托为类型的事件。
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ss.Adding += new Students.AddingEventHandler(ss_Adding);
}
void ss_Adding(object sender, Students.AddingCancelEventArgs e)
{
if (e.Name.StartsWith("a") || ss.Contains(e.Name))//禁止a开头的名字或已有名字
e.Cancel = true;
if (!e.Cancel)
listBox1.Items.Add(e.Name);
else
{
MessageBox.Show("禁止以小写a开头的单词或名称已重复……");
}
}
Students ss = new Students();
private void button1_Click(object sender, EventArgs e)
{
if (((Button)sender).Text == "Add")
{
int age = 0;
if (textBox2.Text != string.Empty) age = textBox2.Text.ToInteger();
if (textBox1.Text != string.Empty)
ss.Add(new Student() { Name = textBox1.Text, Age = age });
}
else
{
MessageBox.Show(ss.Count.ToString());
}
}
}
public static class Extension//扩展方法
{
public static int ToInteger(this string temp)
{
return int.Parse(temp);
}
}
public class Students :CollectionBase
{
public delegate void AddingEventHandler(object sender, AddingCancelEventArgs e);//2、
public event AddingEventHandler Adding;//3、
public class AddingCancelEventArgs : CancelEventArgs//1、
{
public string Name { get; set; }
public AddingCancelEventArgs(bool xancel) : base(xancel) { }
public AddingCancelEventArgs(){ }
}
public void Add(Student temp)
{
bool cancel = false;
if (Adding != null)
{
AddingCancelEventArgs ace = new AddingCancelEventArgs() { Name = temp.Name };
Adding(List, ace);
cancel = ace.Cancel;
}
if (!cancel)
List.Add(temp);
}
public void Remove(Student temp)
{
List.Remove(temp);
}
public Student this[int index]
{
get { return (Student)List[index]; }
set { List[index] = value; }
}
public bool Contains(string name)
{
foreach (Student st in List)
{
if (st.Name == name) return true;
}
return false;
}
}
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
感觉对这个东西懂,知道怎么用。但是却不知道怎么形容。如何描述事件与委托之间的关系呢?谁可以给我一种醍醐灌顶的感觉?感谢!