首页 > 解决方案 > 使用事件处理程序传递更多对象

问题描述

我正在阅读有关通过不使用事件处理程序而是使用委托或通过从事件处理程序中调用其他函数来重载事件处理程序的类似问题。但是我真的看不到如何将委托绑定到自定义控件,就像我在下面的代码中绑定 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);

然后以我的形式1

namespace 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;

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (string item in tempGlobalPaths)
            {
                    GlobalDebugMonitorControl tempGDMcontrol = new GlobalDebugMonitorControl();
                    tempGDMcontrol.Name = item.Split(',')[0];
                    tempGDMcontrol.companyNameLBL.Text = item.Split(',')[0];
                    tempGDMcontrol.globalPathTXT.Text = item.Split(',')[1];
                    tempGDMcontrol.ButtonClick += new EventHandler(UserControl_ButtonClick);
                    flowLayoutPanel1.Controls.Add(tempGDMcontrol);
            }
        }
     }
 }

如您所见,我由发送者创建了一个 tempButton,以根据按下 5 的哪个按钮和 sender.parent.parent 来做一些事情(自定义控件位于另一个面板内的 flowlayout 内的表内等)我终于到达了自定义控件,它告诉我 10 个自定义控件中的哪个按钮被按下了。

所以问题是,有没有办法同时传递发件人(按下的按钮)和曾祖父(拥有发件人按钮的自定义控件)?我的意思是它有效,但这样我现在需要增加多少“世代”。

谢谢你读我。

标签: c#eventhandler

解决方案


您可以介绍自己的 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;

        }

推荐阅读