图片框阵列点击事件

本文关键字:事件 阵列 | 更新日期: 2023-09-27 18:06:21

你好,我是一个编程初学者,我想创建一个简单的国际象棋游戏。我在c#中使用windows窗体。声明和初始化数组没有问题,但是如何为每个图片框设置单击事件呢?在VS属性框中做之前。下面是我的初始化代码。

  public void picbnox()
    {
        picturbox[0, 0] = new PictureBox();
        picturbox[0, 0].Visible = true;
        picturbox[0, 0].Location = new Point(15, 30);
        picturbox[0, 0].Size = new Size(65, 65);
        picturbox[0, 0].BorderStyle = BorderStyle.FixedSingle;
        this.Controls.Add(picturbox[0, 0]);
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                picturbox[i, j] = new PictureBox();
                picturbox[i, j].Visible = true;
                picturbox[i, j].Location = new Point(i *70, j *70);
                picturbox[i, j].Size = new Size(65, 65);
                picturbox[i, j].BorderStyle = BorderStyle.FixedSingle;
                this.Controls.Add(picturbox[i, j]);
            }
        }
    }

图片框阵列点击事件

您可以像这样添加图片框点击事件:

picturebox[0, 0].Click += picturebox_Click; // in your form load event, this is only for one picture box
void picturebox_Click(object sender, EventArgs e)
{
    // do whatever you want to do when the picture box is clicked
}