/// 事件的编写过程:
    
/// 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 { getset; }
            
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 { getset; }

        
public int Age { getset; }

    }

 

 

感觉对这个东西懂,知道怎么用。但是却不知道怎么形容。如何描述事件与委托之间的关系呢?谁可以给我一种醍醐灌顶的感觉?感谢!

相关文章:

  • 2021-11-03
  • 2021-12-17
  • 2021-12-24
  • 2022-12-23
  • 2021-11-04
  • 2021-12-05
  • 2021-12-17
猜你喜欢
  • 2021-07-14
  • 2022-01-11
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
相关资源
相似解决方案