I am trying to add some buttons to a control programmatically using a for-statement.
What I wish to accomplish is that every button fires the click event with the iterator value inside.
Here is an example of my code:
public class Foo
{
public Foo(int val)
{
//Do something with val
}
}
//for-statement
for(int i = 0; i < 5; ++i)
{
var button = new Button();
button.Click += (sender, e) =>
{
var text = new Foo(i);
}
}
In my scenarios it intializes Foo with the last value of the iterator, that being 4.
How can I accomplish to send every value of the iterator?
Button1->Click->Foo(0)
Button2->Click->Foo(1)
... and so on.