![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | ondesic |
I found that I can use a lambda bind a parameter to a function, but I am having a problem. I have 3 buttons in an array called “btns”. I want each button to send a different number to the ButtonDown() function.
If I individually assign each button it will work:
btns[0].ButtonDown += () => _btnDown(0);
btns[1].ButtonDown += () => _btnDown(1);
btns[2].ButtonDown += () => _btnDown(2);
However, if I use a “for” loop to do the same thing, the code doesn’t work. it assigns “4” to every buttons ButtonDown event:
for (int i = 0; i < btns.Length; i++)
{
btns[i].ButtonDown += () => _btnDown(i);
}
Shouldn’t this work?