簡體   English   中英

從 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM