我按照本教程使用表单模板和C#在Visual Studio中创建了一个迷宫,但我不能确切地理解他是如何将图片框变成按钮的,并且能够制作一个允许用户操作图片框来创建迷宫的表单。http://www.c-sharpcorner.com/uploadfile/4a950c/solving-mazes-using-recursion/
private void createNewMaze()
{
mazeTiles = new PictureBox[XTILES, YTILES];
//Loop for getting all tiles
for (int i = 0; i < XTILES; i++)
{
for (int j = 0; j < YTILES; j++)
{
//initialize a new PictureBox array at cordinate XTILES, YTILES
mazeTiles[i, j] = new PictureBox();
//calculate size and location
int xPosition = (i * TILESIZE) + 13; //13 is padding from left
int yPosition = (j * TILESIZE) + 45; //45 is padding from top
mazeTiles[i, j].SetBounds(xPosition, yPosition, TILESIZE, TILESIZE);
//make top left and right bottom corner light blue. Used for start and finish
if ((i == 0 && j == 0) || (i == XTILES - 1 && j == YTILES - 1))
mazeTiles[i, j].BackColor = Color.LightBlue;
else
{
//make all other tiles white
mazeTiles[i, j].BackColor = Color.White;
//make it clickable
EventHandler clickEvent = new EventHandler(PictureBox_Click);
mazeTiles[i, j].Click += clickEvent; // += used in case other events are used
}
//Add to controls to form (display picture box)
this.Controls.Add(mazeTiles[i, j]);
}
}
}我也创建了这个方法,但我只有一个普通的图片框和两个按钮。
private void PictureBox_Click(object sender, EventArgs e)
{
((PictureBox)sender).BackColor = currentColor;
}

发布于 2017-11-24 14:01:53
你的事件处理程序没问题,但是如果你把颜色改成当前的颜色,它不会显示任何变化。
尝尝这个,
private void PictureBox_Click(object sender, EventArgs e)
{
pictureBox.BackColor = Color.Brown;
}https://stackoverflow.com/questions/47466821
复制相似问题