Just create a copy of the variable:
int currentI = i;
button.Click+=new EventHandler ((object sender, EventArgs args) =>
{ button_click (currentI, sender, args); });
Please note that you have a certain number of cracks there. You can write it easier:
int currentI = i;
button.Click += (sender, args) => button_click(currentI, sender, args);
Personally, I renamed the method button_clickto comply with the .NET naming conventions.
source
share