Home > Blockchain >  Cant create a grid inside panel box
Cant create a grid inside panel box

Time:03-29

How would I create a 6x7 grid inside of a panel box in windows form C# on visual studio. I have tried using DrawLine, and Graphics but it is not working.

May someone assist me?

When running the program I was hoping it would look like this:

CodePudding user response:

Hopefully your code looked something like:

private void Form1_Load(object sender, EventArgs e)
{
    panel1.SizeChanged  = Panel1_SizeChanged;
    panel1.Paint  = Panel1_Paint;
}

private void Panel1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    int cols = 7;
    int rows = 6;
    int width = panel1.Width / cols;
    int height = panel1.Height / rows;
    for(int col=1; col<cols; col  )
    {
        e.Graphics.DrawLine(Pens.Black, new Point(col * width, 0), new Point(col * width, panel1.Height));
    }
    for(int row=1; row<rows; row  )
    {
        e.Graphics.DrawLine(Pens.Black, new Point(0, row * height), new Point(panel1.Width, row * height));
    }
}

private void Panel1_SizeChanged(object sender, EventArgs e)
{
    panel1.Invalidate();
}
  • Related