using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region "变量定义" IList<Person> p = new List<Person>() { new Person { Name = "lizeren", Address = "wenzhou", Res = "ailili", Sex=SexType.男 } , new Person {Name="wanglili",Address="jilin",Res="aizeren",Sex=SexType.女 }, new Person {Name="xuhongfei",Address="wenzhou",Res="py",Sex=SexType.女 }, new Person("lizeren","wenzhou","ailili","男") }; BindingSource bds = null; #endregion private void Form1_Load(object sender, EventArgs e) { bds = new BindingSource(); bds.DataSourceChanged += new EventHandler((o, ex) => { this.dataGridView1.DataSource = bds; }); bds.DataSource = p;
委托定义了一个函数指针,这个函数指针来定一个一个变量的时候,给它赋值的是一堆具有相同签名的函数,可以用一个变量动态调用不同的函数,提供极大的便利,比如把一个函数当作参数传入另一个函数的时候。事件则是一种操作系统的驱动模式,类似消息驱动,其中使用了委托这种语法形式,具体可以看这篇博文
http://blog.csdn.net/todototry/archive/2010/01/04/5129415.aspx
用了函数指针,是先取得函数指针包含的值,然后jmp(call)到那个值的地址之中多了一层的传递,就多了一层的灵活性而委托是一个类型安全的函数指针实例
比如你的类里有个int类型的属性NumA
int a;
public int A
{
set{ a = value;}
}
现在我们来做一个捕获这个属性A值改变的事件,事件名称定为NumChanged,下面就可以开始了,很简单public delegate MyDelegate(object obj,EventArgs e); //定义委托
public event MyDelegate NumChanged; //定义事件
private void OnNumChange() //事件监听
{
if(NumChanged != null)
{
NumChanged(obj,e);
}
}最后,我们把事件监听放在属性赋值语句的后面public int A
{
set{
a = value;
OnNumChange(); //这样就完成了一个NumChanged,因为是在赋值后触发的这个事件,所以我们用过去式命名
}
}最后再说一遍,这个事件的作用就是在属性A改变后,做某些处理
委派就是可以让你把方法当做方法的参数进行使用
.net封装了大部分事件
很少要自己去写事件
这恐怕是完全误会了编译器了,这个误会会产生很多似是而非的结论。Action d;
d=abc;.....
d();
.....
}private void abc()
{
....
}以上代码中,并不是给d赋值以函数abc,编译器实际编译出来的代码仍然是类似这样的形式:[code=C#]Action d;
d=new Action(abc);即,委托对象d(或者说成“变量d所引用的委托对象”才更准确一些)被用一个Action类型的对象实例赋值,而这个对象使用指向函数abc的指针(这个时候才用到指针概念)来实例化。因此,委托在及其复杂的结构中使用到了一个或者无数个函数指针,委托仍然是面向对象的,它自己并不就是一个函数指针。
public event MyDelegate NumChanged; //定义事件
这里,MyDelegate 其实是一个class,而NumChanged则只是一个引用 MyDelegate 类型的对象的变量。
factorial = n =>
{
if (n == 0)
return 1;
else
return n * factorial(n - 1);
};
而求出9的阶乘就可以这样:int result=factorial(9);这里,计算阶乘的方法被一个变量factorial传递着。在获取result值之前,获取有人修改了factorial变量所引用的对象,用另外一个Delegate来代替了上面定义的Delegate。
因为Delegate很好玩也很强大。我上一次讨论这个阶乘Delegate,就是因为用来说明函数编程语言也没有什么新鲜的、c#早就有那种能力。
对于菜鸟来说,喜欢简单的例子和通俗的语句
http://www.cnblogs.com/jimmyzhang/archive/2007/09/23/903360.html
看看上面这篇文章吧!!!相信你一定受益匪浅!!!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region "变量定义"
IList<Person> p = new List<Person>()
{
new Person { Name = "lizeren", Address = "wenzhou", Res = "ailili", Sex=SexType.男 } ,
new Person {Name="wanglili",Address="jilin",Res="aizeren",Sex=SexType.女 },
new Person {Name="xuhongfei",Address="wenzhou",Res="py",Sex=SexType.女 },
new Person("lizeren","wenzhou","ailili","男")
};
BindingSource bds = null;
#endregion
private void Form1_Load(object sender, EventArgs e)
{
bds = new BindingSource();
bds.DataSourceChanged += new EventHandler((o, ex) =>
{
this.dataGridView1.DataSource = bds;
});
bds.DataSource = p;
//this.dataGridView1.DataSource = bds;
}
Form2 Frm = null; //添加数据 private void toolStripLabel1_Click(object sender, EventArgs e)
{
if (Frm == null)
{
Frm = new Form2();
Frm.Text = "添加数据"; #region "事件"
//添加数据行
Frm.button1.Click += new EventHandler((sen, eb) =>
{
if (Frm.PersonValue != null)
{
bds.Add(Frm.PersonValue);
Frm.Close();
}
else
{
MessageBox.Show(this, "格式不正确");
}//if
});
//
//关闭按钮
//
Frm.button2.Click += new EventHandler((se, eb) =>
{
Frm.Close();
}); Frm.FormClosed += new FormClosedEventHandler((send, ec) =>
{
Frm = null;
});
#endregion Frm.Show(this);
}
else
{
Frm.Activate();
}//if
}
//修改数据
private void toolStripLabel2_Click(object sender, EventArgs e)
{
if (Frm == null)
{
Frm = new Form2();
Frm.Text = "修改数据";
if (bds.Current != null)
{
Frm.PersonValue = (Person)bds.Current;
} #region "事件"
//
//修改数据
//
Frm.button1.Click += new EventHandler((sen, ex) =>
{
Person CurPer = (Person)bds.Current;
Person gp = Frm.PersonValue;
if (gp != null)
{
CurPer.Name = gp.Name;
CurPer.Address = gp.Address;
CurPer.Res = gp.Res;
CurPer.Sex = gp.Sex;
bds.CurrencyManager.Refresh();
Frm.Close();
}
else
{
MessageBox.Show(this, "格式不正确");
}
});//event
//
//关闭Form2
//
Frm.button2.Click += new EventHandler((se, eb) =>
{
Frm.Close();
});
Frm.FormClosed += new FormClosedEventHandler((send, ec) =>
{
Frm = null;
});
#endregion Frm.Show(this);
}
else
{
Frm.Activate();
}
} private void toolStripButton1_Click(object sender, EventArgs e)
{
IList<Person> pl = (IList<Person>)bds.List;
} private void toolStripButton2_Click(object sender, EventArgs e)
{
bds.DataSource = new List<Person>();
} private void toolStripButton3_Click(object sender, EventArgs e)
{
bds.DataSource = p;
}
}
}