0

当我点击一个按钮时,我想调用我的绘画事件。我有这个:

private void btnDrawMap_Click(object sender, EventArgs e)
        {
            rows = Convert.ToInt32(this.numRows);
            cols = Convert.ToInt32(this.numCols);


            defineCellWallsList();
            defineCellPositionsList();
            pictureBox1_Paint_1;
        }

我不认为你可以这样称呼它。所做pictureBox1_Paint_1的就是:

private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
        {
            myGameForm.drawMap(e, mapCellWallList, mapCellPositionList);
        }

所以如果我可以打电话myGameForm.drawMap,那很好。这是:

public void drawMap(PaintEventArgs e, List<List<int>> walls, List<List<int>> positions)
        {
            // Create a local version of the graphics object for the PictureBox.
            Graphics g = e.Graphics;

            // Loop through mapCellArray.
            for (int i = 0; i < walls.Count; i++)
            {
                int[] mapData = getMapData(i, walls, positions);
                int column = mapData[0];
                int row = mapData[1];
                int right = mapData[2];
                int bottom = mapData[3];

                g.DrawLine(setPen(right), column + squareSize, row, column + squareSize, row + squareSize);
                g.DrawLine(setPen(bottom), column + squareSize, row + squareSize, column, row + squareSize);
            }
            drawMapEdges(e, walls, positions);
        }

它需要一个paintEventArg并且btnDrawMap_Click需要一个eventArg,所以我将btnDrawMap_ClickeventArg参数更改为paintEventArg,但是它有一个错误说明btnDrawMap_Click没有重载采用eventHandler的方法。

任何帮助表示赞赏,谢谢。

4

1 回答 1

4

只需调用Invalidate(),这会强制PictureBox重绘自身,从而调用您的Paint事件:

pictureBox1.Invalidate();
于 2013-09-12T03:56:26.923 回答