/// form1为基类
namespace WindowsApplication1
{
public partial class Form1 : Form
{
//我自定义的事件
public event EventHandler myevent;
public Form1()
{
InitializeComponent();
}
public void myfunc(int a, int b)
{
if (myevent != null)
{
myevent(null , null);
}
}
}
}
/// form2从form1派生,form2为执行类
namespace WindowsApplication1
{
public partial class Form2 : Form1
{
Form1 f = new Form1();
public Form2()
{
InitializeComponent();
f.myevent += new System.EventHandler(diaoyong);
}
public void diaoyong(object o,EventArgs e)
{
MessageBox.Show ("派生类执行");
}
private void button1_Click(object sender, EventArgs e)
{
f.myfunc(1, 1);
}
}
}
运行结果是 : 弹出对话框"派生类执行";
public Form2()
{
InitializeComponent();
f.myevent += new System.EventHandler(diaoyong);//这里是注册
}
myevent(null , null);//这是执行,调用的就是所注册的 diaoyong()
[此贴子已经被作者于2006-12-1 19:47:05编辑过]