EventHandler with custom arguments
Asked Answered
B

4

14

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. In this case, I have a method named "CreateDataContextMenu" that creates a context menu on a given TreeView control.

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

TreeView owner is the control in which I will associate my context menu.

Then later on I add a Click event to a MenuItem like this:

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.

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. Am I going in the right direction with that?

Breger answered 23/6, 2011 at 16:29 Comment(1)
Doesn't owner here contain a reference to the Treeview? Or would it stop at the context menu?Bamberg
T
43

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

menuItemFolder.Click += (sender, e) => YourMethod(owner, dataType);
Ternopol answered 23/6, 2011 at 16:32 Comment(7)
This method works like a charm, it's short and very efficient. The more I ask, the more I learn :) Thank you very much SLaks!Breger
Clearer method by far. For those who are not used to lambda expressions, like me, maybe it would be a good idea to show that 'sender' and 'e' can be passed to YourMethod too.Ajani
How to unsubscribe the same event?Irredeemable
@vijay.k: Store the lambda in a variable.Ternopol
@Ternopol How exactly unsubscribe? Using something Func<int,int,int> f= (a,b)=>a+b; needs a return value.Psychic
@VassilisGr: You need to use a delegate that doesn't return a value. Use the same delegate type as the event.Ternopol
@Ternopol I need to pass custom params into a .Click event and be able to unsubscribe too. I don't quite understand if you mean a delegate and lambda in conjunction or something else.Psychic
C
0

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

Configuration answered 23/6, 2011 at 16:50 Comment(0)
E
0

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
    }
Electorate answered 19/2, 2018 at 7:57 Comment(0)
T
0

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);
Therein answered 14/3, 2021 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.