C# - 调用绘制事件处理程序

use*_*079 0 c# event-handling

我想在单击按钮时调用我的绘画事件。我有这个:

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


            defineCellWallsList();
            defineCellPositionsList();
            pictureBox1_Paint_1;
        }
Run Code Online (Sandbox Code Playgroud)

我认为你不能这样称呼它。所做的一切pictureBox1_Paint_1就是:

private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
        {
            myGameForm.drawMap(e, mapCellWallList, mapCellPositionList);
        }
Run Code Online (Sandbox Code Playgroud)

所以如果我可以打电话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);
        }
Run Code Online (Sandbox Code Playgroud)

它需要 apaintEventArg并且btnDrawMap_Click需要一个 eventArg,因此我将btnDrawMap_ClickeventArg 参数更改为 PaintEventArg,但是它有一个错误,指出btnDrawMap_Click尚未重载采用 eventHandler 的方法。

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

Sim*_*ead 5

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

pictureBox1.Invalidate();
Run Code Online (Sandbox Code Playgroud)