C# pass additional parameter to an event handler while binding the event at the run time
Solution 1
You can use anonymous delegate for that:
lnkSynEvent.Click +=
new EventHandler((s,e)=>lnkSynEvent_Click(s, e, your_parameter));
Solution 2
I don't know exactly when it's changed, but now it's even easier!
lnkSynEvent.Click += (s,e) => lnkSynEvent_Click(s, e, your_parameter);
Solution 3
EventHandler myEvent = (sender, e) => MyMethod(myParameter);//my delegate
myButton.Click += myEvent;//suscribe
myButton.Click -= myEvent;//unsuscribe
private void MyMethod(MyParameterType myParameter)
{
//Do something
}
Solution 4
by use of delegate:
lnkbtnDel.Click += delegate(object s, EventArgs e1) {
Dynamic_Click(s, e1, lnkbtnDel.ID);
};`
Solution 5
All answers above seem to be fine, but I have discovered one pitfall which is not so obvious and it took some time to figure out what is going on, so I wanted to share it.
Assume that myList.Count
returns 16.
In the following case, OnValueChangeWithIndex(p1, p2, i)
will always be called with i = 16.
for (int i = 0; i < myList.Count; i++)
{
myList[i].OnValueChange += (p1, p2) => OnValueChangeWithIndex(p1, p2, i);
}
To avoid that, you would need to initialize a new variable inside of the loop, then pass the new variable to the function.
for (int i = 0; i < myList.Count; i++)
{
int index = i;
myList[i].OnValueChange += (p1, p2) => OnValueChangeWithIndex(p1, p2, index);
}
Closures close over variables, not over values.
Related videos on Youtube
foo-baar
Updated on March 04, 2022Comments
-
foo-baar about 2 years
I have a link button which have a regular click event :
protected void lnkSynEvent_Click(object sender, EventArgs e) { }
And I bind this event at the runtime :
lnkSynEvent.Click += new EventHandler(lnkSynEvent_Click);
Now I need the function to accept additional argument:
protected void lnkSynEvent_Click(object sender, EventArgs e, DataTable dataT) { }
And pass the same as parameter while binding this event :
lnkSynEvent.Click += new EventHandler(lnkSynEvent_Click, //somehow here);
Not sure how to achieve this. Please help.
Thanks in advance.
Vishal
-
Paulie Waulie almost 11 yearsWhere is the DataTable going to come from?
-
foo-baar almost 11 years@PaulieWaulie its there where I am binding the event.
-
-
foo-baar almost 11 yearsWOW worked like a charm, thanks @alex, tough I am not much famalier with anonymous delegates yet.
-
Microsoft DN almost 11 yearsYou should mark this answer as correct. This may help others.
-
Alexandr Sargsyan over 8 yearsSimply you can write like this Object.SomeEvent+= (sender,e)=>SomeEventHandler(sender, e, "Data"); /// And define your event handler like this void SomeEventHandler(object sender, SomeEventArgs e, string data);
-
Stunt about 8 yearsThis actually worked better in my scenario as i needed to map an expected EventHandler<a,b> to EventHandler<a,b,c>. The accepted answer did not resolve this scenario.
-
T.Todua over 6 yearsAlex, and how can we remove that? simply with
lnkSynEvent.Click -=new EventHandler( ...
? -
testing almost 6 years@alex: As T.Todua stated, how do you unsubscribe from it?