一個類中定義如下事件:
[csharp]
- public delegate void AfterDrawPointEvent(object sender,double x,double y);
[csharp]
- private AfterDrawPointEvent pAfterDrawPointEvent;
- /// <summary>
- /// 插入要素事件
- /// </summary>
- public event AfterDrawPointEvent AfterDrawpoint
- {
- add
- {
- pAfterDrawPointEvent += value;
- }
- remove
- {
- pAfterDrawPointEvent -= value;
- }
- }
- private void RaiseAfterDrawPointEvent(object sender, double x,double y)
- {
- if (pAfterDrawPointEvent != null)
- {
- pAfterDrawPointEvent(sender,x,y);
- }
-
- }
然後定義實現的響應方法,注意要先去掉舊的響應方法,否則可能出現部分變量的狀態無法更新。代碼如下:
[csharp]
- m_pDrawCustomPoint.AfterDrawpoint-= new DrawCustomPoint.AfterDrawPointEvent(m_pDrawCustomPoint_AfterDrawpoint);
- m_pDrawCustomPoint.AfterDrawpoint += new DrawCustomPoint.AfterDrawPointEvent(m_pDrawCustomPoint_AfterDrawpoint);