I have a piece of code that filters a list using LINQ, creates a list of instances of an anonymous type, and assigns an event handler to each instance:
// Select every linear expression and create a menu item from it
var items = from expr in expressionList.Expressions
where expr.Type == ExpressionType.Linear
let stdExpr = (StandardExpression)expr
select new
{
Menu = new ToolStripMenuItem(stdExpr.Expression), // string
stdExpr.Slot // int
};
// Wire a Click event handler to each menu to set the tracked line
foreach (var item in items)
{
item.Menu.Click += (s, e) => graph.SetTrackedLine(item.Slot);
menuTrackLineWithMouse.DropDownItems.Add(item.Menu);
}
This works well in that the event handlers get wired and the menus get added correctly. The problem comes when the menu item is clicked, and the handler is fired. No matter what menu item fired the handler, only the last one is ever passed to SetTrackedLine
.
An example is if I have two menus, "sin(x)", with slot 0
, and "cos(x)", with slot 1
, both Click
events pass 1
to SetTrackedLine
, no matter if "sin(x)" was clicked or "cos(x)" was.
My question is, why does this happen? Shouldn't item.Slot
refer to each separate instance of the anonymous type?
Thanks.