[英]what is the difference between Control.OnClick and Control.InvokeOnClick
[英]Identifying sender from Control.InvokeOnClick()
我正在編寫一個掃雷應用程序,如果我打開周圍有 0 個地雷的磁貼,我會嘗試從單擊事件遞歸地引發單擊事件。 我需要使用InvokeOnClick()
弄清楚遞歸調用的發送方是否會是什么。
picture1.Click += (sender,e) => {//some switch case
case 0: //when 0 mines are around
InvokeOnClick(picture2,e);
}
參考資料揭曉答案。 InvokeOnClick
調用目標控件的受保護方法OnClick
,然后調用Click
事件,並this
(等於目標控件)作為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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.