How do I parameterize an event in C #

I want to override the event, I mean that I have this:

boton.Click += infoApp; //i think thats similar to: boton.Click = new System.EventHandler(infoApp). 

when the boton button is pressed, the triggers of the infoApp function / method, this method looks something like this:

 private void infoApp(object sender, EventArgs e) { /* my code*/ } 

Evolution is still going well; but I NEED to send another parameter to this method:

 boton.Click += infoApp(string par) 

so I thought this might work:

 private void infoApp(object sender, EventArgs e, string par) { /*My code*/ } 

but this is not so.

I read things like delegates, but I don't understand; and I don’t know what to do to solve my problem; any ideas?

Thanks in advance

pd: sorry my terrible english, i'm not english. Just try to explain. I am using VS2008.

+4
source share
1 answer

One way to solve this problem is to wrap the event handler in closure:

Instead of this line:

 boton.Click += infoApp; 

Do it:

 string par = "something"; boton.Click += (sender, e) => { //Now call your specific method here: infoApp(sender, e, par); }; 
+9
source

Source: https://habr.com/ru/post/1411533/


All Articles