I am using Visual Studio 2017 and C# to create a Win Form application. I am trying to add mouse enter behaviour to a set of controls as follows;
int i = 0;
foreach(MetroTileItem item in game_tile_panel.Items){
// THIS MESSAGE BOX DISPLAYS THE CORRECT VALUE
MessageBox.Show(i.ToString());
item.MouseEnter += (Object sender, EventArgs e) => {
// THIS MESSAGE BOX DOES NOT DISPLAY TO CORRECT VALUES
MessageBox.Show(i.ToString());
};
i++;
}
The first message box returns the value of i
for each item and displays correctly. The second message box, inside of the MouseEnter
behaviour does not display the correct values for i
, or atleast not the values I am expecting.
When I perform the MouseEnter
behaviour at runtime the message box displays the value 3
for every item. Whereas I would expect the first item to display 0
, the second item 1
ect.
Can anyone shed should light on why this is happening and how I might fix it. Perhaps I am not able to add behaviours in this manner? Thanks