public void GridCreate()
{
Graphics g = pictureBox1.CreateGraphics();
SolidBrush brushBlack = new SolidBrush(Color.Black);
Rectangle[,] block = new Rectangle[16, 16];
for (int i = 0; i <= block.GetLength(0) - 1; i++)
{
for (int n = 0; n <= block.GetLength(0) - 1; n++)
{
block[n, i] = new Rectangle(i * blockSize, n * blockSize, 20, 20);
g.FillRectangle(brushBlack, block[n, i]);
}
}
data.block = block;
}
private void Form1_Shown(object sender, EventArgs e)
{
GridCreate();
}
I'm trying to make a grid in WindowsForms using PictureBox, but related code is not working correctly.This data.block = block;
part works, but this g.FillRectangle(brushBlack, block[n, i]);
doesn't work at all. I think the problem is in the Form1_Shown
event, because this:
private void Form1_Click(object sender, EventArgs e)
{
GridCreate();
}
executes perfectly fine.
Override protected override void OnShown(EventArgs e)
gives the same result as Form1_Shown
.
The problem is
CreateGraphics()
, which is a temporary surface that gets erased when the PictureBox refreshes itself.Just create the grid once, then draw the data in the Paint() event: