简体   繁体   English

具有自定义 arguments 的事件处理程序

[英]EventHandler with custom arguments

I've been looking for an answer for about an hour on Google but I did not found exactly what I'm looking for.我一直在谷歌上寻找答案大约一个小时,但我没有找到我要找的东西。

Basically, I have a static Helper class that helps perform many things I do frequently in my App.基本上,我有一个 static Helper class 帮助执行我在应用程序中经常执行的许多操作。 In this case, I have a method named "CreateDataContextMenu" that creates a context menu on a given TreeView control.在这种情况下,我有一个名为“CreateDataContextMenu”的方法,它在给定的 TreeView 控件上创建上下文菜单。

public static void CreateDataContextMenu(Form parent, TreeView owner, string dataType)
{ ... }

TreeView owner is the control in which I will associate my context menu. TreeView 所有者是我将在其中关联上下文菜单的控件。

Then later on I add a Click event to a MenuItem like this:稍后我将 Click 事件添加到 MenuItem,如下所示:

menuItemFolder.Click += new System.EventHandler(menuItemFolder_Click);

The problem I have here is that I want to pass "owner" and "dataType" as arguments to the menuItemFolder_Click event.我在这里遇到的问题是我想将“所有者”和“数据类型”作为 arguments 传递给 menuItemFolder_Click 事件。

I tried the following:我尝试了以下内容:

menuItemFolder.Click += new System.EventHandler(menuItemFolder_Click(sender,e,owner,dataType));
(...)
private static void menuItemFolder_Click(object sender, System.EventArgs e, Treeview owner, string dataType)
{...}

But it doesn't work at all.但它根本不起作用。 It might be very naive of me to do it that way but I"m not very comfortable with event handler yet.这样做对我来说可能非常天真,但我对事件处理程序还不是很满意。

Any idea on how I could do that?关于我该怎么做的任何想法? My first guess is that I need to create my own EventHandler for this specific case.我的第一个猜测是我需要为这个特定案例创建我自己的 EventHandler。 Am I going in the right direction with that?我会朝着正确的方向前进吗?

You should create a lambda expression that calls a method with the extra parameters:您应该创建一个lambda 表达式来调用带有额外参数的方法:

menuItemFolder.Click += (sender, e) => YourMethod(owner, dataType);

I think the simplest code would be this:我认为最简单的代码是这样的:

    EventHandler myEvent = (sender, e) => MyMethod(myParameter);//my delegate

    myButton.Click += myEvent;//suscribe

    private void MyMethod(MyParameterType myParameter)
    {
     //Do something 

     //if only one time
     myButton.Click -= myEvent;//unsuscribe
    }

Honest admission up front: I have not tried the code below.坦白承认:我没有尝试过下面的代码。

I think the reason我觉得原因

menuItemFolder.Click += new System.EventHandler(menuItemFolder_Click(sender,e,owner,dataType));

won't work is because you are actually passing to System.EventHandler () the result of the invocation of menuItemFolder_Click () with the parameters provided.不起作用是因为您实际上是将使用提供的参数调用 menuItemFolder_Click () 的结果传递给 System.EventHandler ()。 You are not passing a pointer to the function itself.您没有将指针传递给 function 本身。

Try to write another function that implements the details of menuItemFolder_Click ().尝试再写一个function实现menuItemFolder_Click()的细节。 See if something like看看有没有类似的

private void menuItemFolder_Click_Helper (object sender, EventArgs e, object Owner, object DataType) {
// implement details here
}

and then call the function from within menuItemFolder_Click ().然后从 menuItemFolder_Click () 中调用 function。

Passing custom args into an event handler is not too difficult.将自定义参数传递给事件处理程序并不难。 Below is a clean and easily reusable method of doing so.下面是一种干净且易于重用的方法。 Check it:核实:

public class MyClass
{
    public CustomArgsEventHandler MyEvent1;
    public MyClass(){MyEvent1+=observer;}
    public void observer(object sender, CustomEventArgs e){print(e.myArg);}
    //...
}

//place in the same file if you like!
public class CustomEventArgs : EventArgs
{
    public float myArg {get;set;}
    public CustomEventArgs (float d) { myArg = d; }
}
public delegate void CustomArgsEventHandler (object sender, CustomEventArgs e);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM