The following code creates a 5 by 5 grid of buttons from an array. I'm useing lambda expression to get the position of the button in array at the moment of mouse click. The problem is, that i need to know whether left or right mouse button were pressed. So far i have output from left button, but no response from right.
Button[] grid5x5 = new Button[25];
void Spawn5x5Grid()
{
lblSize.Text = "5x5";
currentGridSize = GridSize.grid5x5;
currentGameState = GameState.GameOn;
// Position of the first button
int x = 50, y = 150;
// Index of the button for the loop
int count = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
grid5x5[count] = new Button
{
Size = new Size(31,31),
Location = new Point(x, y)
};
this.Controls.Add(grid5x5[count]);
grid5x5[count].MouseClick += (o, ee) =>
{
Button button = o as Button;
int index = Array.IndexOf(grid5x5, button);
if (ee.Button == MouseButtons.Left)
{
Console.WriteLine("Left button");
}
else if (ee.Button == MouseButtons.Right)
{
Console.WriteLine("Right button");
}
};
count++;
x = x + 31;
}
x = 50;
y = y + 31;
}
}