0

I've drawn a grid on a picturebox with .drawline I then let users click a cell on this grid to change the colour. I'm doing this through setting a value for colour for each cell (clicking sets it to 1)

The only way I can get it to update is on mouse up which causes problems with the pathfinding algorithms I'm testing. I tried putting it on a timer but it flickers too much.

You can see the problem here The pathfinding algorithm finishes but the grid doesn't update until I click the grid.

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            GetX(this, e);
            GetY(this, e);
            if (DrawCase == "Wall") {
                if (gridmatrix[XClick, YClick].COLOUR == 0) //If tile is white and left mouse is pressed
                {
                    gridmatrix[XClick, YClick].COLOUR = 1; //Set colour to 1 (place a wall)
                }
                else if (gridmatrix[XClick, YClick].COLOUR == 1) //If tile is a wall and left mouse is pressed
                {
                    gridmatrix[XClick, YClick].COLOUR = 0; //Set to empty
                }
        }

Grid is updated on MouseUp... (How else could I update the grid?)

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        mousedown = false;
        int rowcolumn = Convert.ToInt32(textBox1.Text); //Gets the dimensions from the users input

        cellsize = (pictureBox1.Width / rowcolumn);
        if (pictureBox1.Image == null)//if no available bitmap exists on the picturebox to draw on

        {
            //create a new bitmap
            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);

            pictureBox1.Image = bmp; //assign the picturebox.Image property to the bitmap created

        }


        for (int i = 0; i < gridmatrix.GetLength(0); i++)
        {
            for (int j = 0; j < gridmatrix.GetLength(1); j++)
            {
                Rectangle rect = new Rectangle(gridmatrix[i, j].X * cellsize, reverseYvalues[gridmatrix[i, j].Y] * cellsize, cellsize, cellsize); //Positioning and sizing for the rectangle



                if (gridmatrix[i, j].COLOUR == 0)
                {
                    using (Graphics g = Graphics.FromImage(pictureBox1.Image))
                    {
                        g.FillRectangle(Brushes.White, rect);
                    }
                }
                else if (gridmatrix[i, j].COLOUR == 1)
                {
                    using (Graphics g = Graphics.FromImage(pictureBox1.Image))
                    {
                        g.FillRectangle(Brushes.Blue, rect);
                    }


                }

            }
        }
    }
Seitzy
  • 23
  • 1
  • 4
  • 1
    Drawing should be done in the Paint event – Ňɏssa Pøngjǣrdenlarp Nov 28 '20 at 13:32
  • I cant draw the grid that way because I need the dimensions from the textbox first – Seitzy Nov 28 '20 at 13:39
  • the paint event is only being triggered when I alt tab out of the program. – Seitzy Nov 28 '20 at 13:57
  • The Paint event is triggered by calling pbox.Invalidate, maybe from the pbox_MouseClick event. - But the real question is whether to draw into a Bitmap (from whereever you want) or draw onto the PictureBox (always from the Paint event!). – TaW Nov 28 '20 at 14:48
  • Maybe you want the filling to go slow as in an animation? For this indeed use Paint and a Timer and Invalidate in the Tick drawing always the full grid up to the new number of cells.. – TaW Nov 28 '20 at 14:54
  • As mentioned, move the painting code into the `Paint` event and use the `e.Graphics` to draw. Call `pictureBox1.Invalidate();` method whenever you need to refresh the drawing. From `MouseUp`, `SizeChanged` for example. – dr.null Nov 28 '20 at 14:56
  • "I cant draw the grid that way because I need the dimensions from the textbox first" You can always CHECK in the `Paint()` event if you have the values you need and not draw anything if the user hasn't entered valid values yet... – Idle_Mind Nov 28 '20 at 16:17

0 Answers0