我可以实现一个自定义点击事件,当用户点击我的对象时引发。
我的对象没有继承任何类,但它具有 Location Width Height 属性。
问题是:我不知道创建 MyObj 时如何订阅点击事件的代码。
public delegate void CusEventHandler(object sender, CusEventArgs e);
public class MyObject
{
private Point location;
private int width;
private int height;
public event CusEventHandler Click;
public MyObject(Point loc, int w, int h)
{
this.location = loc;
this.width= w;
this.height= h;
}
protected virtual void OnClick(CusEventArgs e)
{
if (Click != null)
{
Click(this, e);
}
}
}
public partial class Form1 : Form
{
public MyObject myObj;
public Form1()
{
InitializeComponent();
myObj = new (new Point(5,5),100,100);
myObj.Click+= new CusEventHandler(myObj_click);
}
private void myObj_click(object sender,CusEventArgs e)
{
//Some logic
}
}