亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用事件處理程序傳遞更多對象

使用事件處理程序傳遞更多對象

C#
守候你守候我 2023-05-13 16:03:22
我正在閱讀有關通過不使用事件處理程序而是使用委托或通過從事件處理程序中調用其他函數來重載事件處理程序的類似問題。但我真的看不出如何將委托綁定到自定義控件,就像我在下面的代碼中綁定 ButtonClick 一樣。我有一個表單,假設有 10 個自定義控件。每個自定義控件有 5 個按鈕。我從每個自定義控件的每個按鈕傳遞按鍵的方式是:這是在我的自定義控件的 cs 文件 (GlobalDebugMonitorControl.cs)namespace GlobalDebugMonitor{public partial class GlobalDebugMonitorControl : UserControl{    public GlobalDebugMonitorControl()    {        InitializeComponent();    }    public event EventHandler ButtonClick;    private void MultiControl_Click(object sender, EventArgs e)    {        if (this.ButtonClick != null)            this.ButtonClick(sender, e);//**How Do I put here both sender and this**    }}}然后自定義 control.designer.cs 中的所有按鈕都有這樣的東西:this.openFileBTN.Click += new System.EventHandler(this.MultiControl_Click);this.editFilePathBTN.Click += new System.EventHandler(this.MultiControl_Click);this.delControlBTN.Click += new System.EventHandler(this.MultiControl_Click);this.addControlBTN.Click += new System.EventHandler(this.MultiControl_Click);this.editCompanyNameBTN.Click += new System.EventHandler(this.MultiControl_Click);然后在我的 form1namespace GlobalDebugMonitor{     public partial class Form1 : Form    {        protected void UserControl_ButtonClick(object sender, EventArgs e)        {            Button tempButton = (Button)sender;            GlobalDebugMonitorControl tempParentControl = (GlobalDebugMonitorControl)((tempButton.Parent).Parent).Parent;        }如您所見,我由發件人創建了一個 tempButton 來根據 5 的哪個按鈕被按下以及 sender.parent.parent 做一些事情(自定義控件位于另一個面板內的流布局內的表內等) ) 我終于找到了自定義控件,它告訴我按下了 10 個自定義控件中的哪個按鈕。所以問題是,有沒有辦法同時傳遞發件人(按下的按鈕)和曾祖父(擁有發件人按鈕的自定義控件)?我的意思是它有效但這樣我現在需要多少“世代”我需要上升。
查看完整描述

1 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

你可以介紹你自己的 EventArgs 類型


public class CustomEventArgs : EventArgs

    {

        public GlobalDebugMonitorControl Control { get; set; }


        public CustomEventArgs(GlobalDebugMonitorControl control) 

        {

            this.Control = control;

        }

    }

然后更改 eventHandler 以使用它:


public event EventHandler<CustomEventArgs> ButtonClick;

所以調用代碼是:


this.ButtonClick(sender, new CustomEventArgs(this));

當然還有事件的實施者:


protected void UserControl_ButtonClick(object sender, CustomEventArgs e)

        {


            Button tempButton = (Button)sender;

            GlobalDebugMonitorControl tempParentControl = e.Control;


        }


查看完整回答
反對 回復 2023-05-13
  • 1 回答
  • 0 關注
  • 137 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號