public Form1()
{
InitializeComponent();
Collection<Test> tests = new Collection<Test>();
tests.Add(new Test("test1"));
tests.Add(new Test("test2"));
foreach (Test test in tests)
{
Button button = new Button();
button.Text = test.name;
button.Click+=new EventHandler((object obj, EventArgs arg)=>{
this.CreateTest(test);
});
this.flowLayoutPanel1.Controls.Add(button);
}
}
public void CreateTest(Test test)
{
MessageBox.Show(test.name);
}
}
when i click the button witch text is 'test1', the messagebox will show 'test2',but my expect is 'test1'. So ,would anyone please tell me why or what`s wrong with my code.