这个基础知识,我随手写了个模型,可以还原比如WinForm事件的基础流程,只不过它是通过windows消息控制的,这里简化为成员变量(name)控制,希望对你有用 using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication3 { class Program { //一个较为完整的委托-事件 模型应用 //by jinjazz //http://blog.csdn.net/jinjazz //模型表达 //Test对象执行DoTest方法,回调onTest事件,通过参数控制输出 static void Main(string[] args) { Test t1 = new Test(); t1.Name = "t1"; t1.onTest += new Test.dTest(t_onTest); Test t2 = new Test(); t2.Name = "t2"; t2.onTest += new Test.dTest(t_onTest); t1.DoTest("aaaa"); t2.DoTest("bbbb"); Console.Read(); } static void t_onTest(object sender, Test.testEventArgs args) { Test t = sender as Test; if(t.Name=="t1") args.Cancled = true; } } class Test { //委托参数 public class testEventArgs { public bool Cancled=false; } //委托 public delegate void dTest(object sender, testEventArgs args); //事件 public event dTest onTest; //函数 public void DoTest(string s) { if (this.onTest != null) { //参数判断 testEventArgs arg = new testEventArgs(); this.onTest(this, arg); if (arg.Cancled == true) { return; } } Console.WriteLine(s); } //成员变量 或 属性 public string Name = string.Empty;
意思button.onclick+=new eventhandler(委托指向的函数名)
构造函数是一个类似函数指针的东西和一个委托方法表里面的对象类型
以及方法
invoke
begininvoke
endinvoke
等
看看这个一个c#睡前的故事。关于事件和委托的
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication3
{
class Program
{
//一个较为完整的委托-事件 模型应用
//by jinjazz
//http://blog.csdn.net/jinjazz
//模型表达
//Test对象执行DoTest方法,回调onTest事件,通过参数控制输出
static void Main(string[] args)
{
Test t1 = new Test();
t1.Name = "t1";
t1.onTest += new Test.dTest(t_onTest); Test t2 = new Test();
t2.Name = "t2";
t2.onTest += new Test.dTest(t_onTest);
t1.DoTest("aaaa");
t2.DoTest("bbbb"); Console.Read();
}
static void t_onTest(object sender, Test.testEventArgs args)
{
Test t = sender as Test;
if(t.Name=="t1")
args.Cancled = true;
}
}
class Test
{
//委托参数
public class testEventArgs
{
public bool Cancled=false;
}
//委托
public delegate void dTest(object sender, testEventArgs args);
//事件
public event dTest onTest; //函数
public void DoTest(string s)
{
if (this.onTest != null)
{
//参数判断
testEventArgs arg = new testEventArgs();
this.onTest(this, arg);
if (arg.Cancled == true)
{
return;
}
}
Console.WriteLine(s);
} //成员变量 或 属性
public string Name = string.Empty;
}
}