首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让C#中的图片框在visual Studio for maze设计项目中可点击?

如何让C#中的图片框在visual Studio for maze设计项目中可点击?
EN

Stack Overflow用户
提问于 2017-11-24 13:23:40
回答 1查看 399关注 0票数 1

我按照本教程使用表单模板和C#在Visual Studio中创建了一个迷宫,但我不能确切地理解他是如何将图片框变成按钮的,并且能够制作一个允许用户操作图片框来创建迷宫的表单。http://www.c-sharpcorner.com/uploadfile/4a950c/solving-mazes-using-recursion/

代码语言:javascript
复制
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]);
            }
        }
    }

我也创建了这个方法,但我只有一个普通的图片框和两个按钮。

代码语言:javascript
复制
private void PictureBox_Click(object sender, EventArgs e)
 {
        ((PictureBox)sender).BackColor = currentColor;
    }

EN

回答 1

Stack Overflow用户

发布于 2017-11-24 14:01:53

你的事件处理程序没问题,但是如果你把颜色改成当前的颜色,它不会显示任何变化。

尝尝这个,

代码语言:javascript
复制
private void PictureBox_Click(object sender, EventArgs e)
 {
   pictureBox.BackColor = Color.Brown;
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47466821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档