I need someone to help me with this little problem I have. Here's a code fragment:
void shuffle()
{
for (int i = 0; i < 4; i++) {
// here should be the computations of x and y
buttons[i].Click += (s, e) => { show_coordinates(x, y); };
// shuffling going on
}
}
void show_coordinates(int x, int y)
{
MessageBox.Show(x + " " + y);
}
as you can see I create a new event handler with different x and y for each button every time I run the loop. There's another button in my form which shuffles the buttons randomly.
So here's the problem: If I press the shuffle button say 10 times and then press any of the shuffled buttons, event handlers stack up and I get 10 message boxes displaying x and y values.
So how can I overwrite the previous event handler with a new one each time I press shuffle.
Thanks in advance.