我试图动态创建一个按钮数组,但我无法弄清楚如何让函数知道实际按下了哪个按钮。我尝试过这种方式,但似乎没有成功。有任何想法吗?
public void game_setup(int columns, int rows, int mines)
{
//game_destroy();
//Set Window Size
this.Height = 50 + Options.y_ini + rows * (Options.size + Options.space);
this.Width = 20 + 2 * Options.x_ini + columns * (Options.size + Options.space);
//Setup the playing field
Button[,] field = new Button[columns,rows];
for (int i = 0; i < columns; i++)
{
for (int j = 0; j < rows; j++)
{
field[i, j] = new Button();
//Button Size
field[i, j].Width = Options.size;
field[i, j].Height = Options.size;
//Button Position
int x = Options.x_ini + i * (Options.size + Options.space);
int y = Options.y_ini + j * (Options.size + Options.space);
field[i, j].Location = new Point(x, y);
//Event Handler
int send_i = i;
int send_j = j;
field[i, j].Click += (sender, args) =>
{
field_Click(send_i, send_j);
};
//Add the Button to the GameBoard
Controls.Add(field[i, j]);
}
}
//Distribute the Mines
//...
}
public void field_Click(int x, int y)
{
MessageBox.Show("X:" + x + " Y:" + y);
field[1, 2].Text = "hi";
}