我正在编写一个扫雷器应用程序,如果我打开周围有0个矿山的瓷砖,我会尝试从单击事件中递归地提出点击事件。我需要弄清楚使用InvokeOnClick()
是否将是递归调用的发件人。
picture1.Click += (sender,e) => {//some switch case
case 0: //when 0 mines are around
InvokeOnClick(picture2,e);
}
参考源显示答案。InvokeOnClick
呼叫目标控制的受保护方法OnClick
,然后用this
(等于目标控制(将Click
事件作为sender
,这意味着目标控件将是其自己的Click
事件的sender
参数。
// snippet from Reference Source
protected void InvokeOnClick(Control toInvoke, EventArgs e) {
if (toInvoke != null) {
toInvoke.OnClick(e);
}
}
// ...
protected virtual void OnClick(EventArgs e) {
Contract.Requires(e != null);
EventHandler handler = (EventHandler)Events[EventClick];
if (handler != null) handler(this, e);
}
在您的情况下,InvokeOnClick(picture2, e);
将调用picture2.Click
事件,参数sender
设置为picture2
。