使用事件处理程序传递更多对象
Passing more objects with eventhandler
我正在阅读有关通过不使用事件处理程序而是使用委托或通过从事件处理程序中调用其他函数来重载事件处理程序的类似问题。但我真的看不出如何将委托绑定到自定义控件,就像我在下面的代码中绑定 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(自定义控件位于 table 中来做一些事情在另一个面板内的流程布局内等)我终于找到了自定义控件,它告诉我按下了 10 个自定义控件中的哪个按钮。
所以问题是,有没有办法同时传递发件人(按下的按钮)和曾祖父(拥有发件人按钮的自定义控件)?我的意思是它有效,但这样我现在需要多少 "generations" 我需要上升。
感谢您阅读我。
你可以引入你自己的 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;
}
我正在阅读有关通过不使用事件处理程序而是使用委托或通过从事件处理程序中调用其他函数来重载事件处理程序的类似问题。但我真的看不出如何将委托绑定到自定义控件,就像我在下面的代码中绑定 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(自定义控件位于 table 中来做一些事情在另一个面板内的流程布局内等)我终于找到了自定义控件,它告诉我按下了 10 个自定义控件中的哪个按钮。
所以问题是,有没有办法同时传递发件人(按下的按钮)和曾祖父(拥有发件人按钮的自定义控件)?我的意思是它有效,但这样我现在需要多少 "generations" 我需要上升。
感谢您阅读我。
你可以引入你自己的 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;
}