user2602079
user2602079

Reputation: 1393

C# - calling the paint event handler

I want to call my paint event when i click a button. I have this:

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


            defineCellWallsList();
            defineCellPositionsList();
            pictureBox1_Paint_1;
        }

I don't think you can call it in this way. All pictureBox1_Paint_1 does is:

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

So if i can call myGameForm.drawMap instead, that would be fine. Which is:

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);
        }

It requires a paintEventArg and the btnDrawMap_Click requires an eventArg, so i changed the btnDrawMap_Click eventArg parameter to paintEventArg, however it has an error stating that btnDrawMap_Click has not overloaded method that takes an eventHandler.

Any help appreciated, thanks.

Upvotes: 0

Views: 5246

Answers (1)

Simon Whitehead
Simon Whitehead

Reputation: 65049

Just call Invalidate(), which forces the PictureBox to redraw itself, thereby calling your Paint event:

pictureBox1.Invalidate();

Upvotes: 5

Related Questions