namespace WindowsFormsApplication7
{
public class Class_myEvent
{
public delegate void myEventHandler(object sender, System.EventArgs e);//定义一个委托
public void myEventFunction(object sender, System.EventArgs e)//定义事件响应函数
{
MessageBox.Show("my event is OK!", "提示");
}
public event myEventHandler myEventObject;//定义事件对象
public void onEvent()
{
myEventObject = new myEventHandler(myEventFunction);//绑定事件响应函数
EventArgs e = new EventArgs();
myEventObject?.Invoke(this, e);//触发事件
}
}
}
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Class_myEvent myEvent = new Class_myEvent();
myEvent.onEvent();
}
}
}
namespace WindowsFormsApplication7{ public class Class_myEvent { public delegate void myEventHandler(object sender, System.EventArgs e);//定义一个委托 public void myEventFuncti