无法在面板框内创建网格

Cant create a grid inside panel box

如何在 visual studio 上以 windows 形式的 C# 在面板框内创建 6x7 网格。我试过使用 DrawLine 和 Graphics,但它不起作用。

有人可以帮助我吗?

当 运行 我希望的程序看起来像这样:

希望您的代码类似于:

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