美好的一天,我是编程的初学者,我想创建一个简单的国际象棋游戏。我在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]);
}
}
}
答案 0 :(得分:1)
您可以像这样添加图片框点击事件:
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
}