Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 616
Sasha Avatar asked Jun 23 '11 16:06

Sasha


1 Answers

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

menuItemFolder.Click += (sender, e) => YourMethod(owner, dataType);
like image 95
SLaks Avatar answered Oct 19 '22 04:10

SLaks