Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I parameterize an event, in c#

Tags:

c#

events

i want to redefine an event, i mean, i have this:

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

when button 'boton' is clicked, the function/method 'infoApp' triggers, this method its something like this:

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

Until here, evetithing goes well; but I NEED to send another parameter to that method:

boton.Click += infoApp(string par)

so i thought that this could work:

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

but it doesn't.

I've readen things like delegates but i don't understand; and i dont know what to do in order to solve my problem; any ideas?

Thanks in advance

pd: sorry by my terrible english, i'm not english speaker. Try to be simple explaining. I'm using VS2008.

like image 781
karlos9o Avatar asked Jan 17 '23 17:01

karlos9o


1 Answers

One way you could solve this, would be to wrap your event handler in a closure:

Instead of this line:

boton.Click += infoApp;

Do this:

string par = "something";

boton.Click += (sender, e) => {
  //Now call your specific method here:
  infoApp(sender, e, par);
};
like image 165
Dave Bish Avatar answered Jan 26 '23 04:01

Dave Bish