EventHandler with custom arguments

♀尐吖头ヾ 提交于 2019-11-27 22:20:51

You should create a lambda expression that calls a method with the extra parameters:

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

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. You are not passing a pointer to the function itself.

Try to write another function that implements the details of 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 ().

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
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!