EventHandler with custom arguments

后端 未结 3 1728
自闭症患者
自闭症患者 2020-12-05 20:21

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 pe

相关标签:
3条回答
  • 2020-12-05 20:56

    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
        }
    
    0 讨论(0)
  • 2020-12-05 21:02

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

    menuItemFolder.Click += (sender, e) => YourMethod(owner, dataType);
    
    0 讨论(0)
  • 2020-12-05 21:06

    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 ().

    0 讨论(0)
提交回复
热议问题