1

所以我有我的代码在 flowLayoutPanel 内绘制矩形和圆形。之后我已经做到了,当您单击 flowLayoutPanel 内部时,您选择的矩形显示在第二个 flowLayoutPanel 的左侧,如下图所示:

单击位置 [0,0] 的矩形时

但问题是,无论何时单击任何其他位置,即 [0,1] 或 [2,3],都不会显示所选位置。我也知道该数组可以正常工作,因为当我将代码更改为flowLayoutPanel2.CreateGraphics();鼠标flowLayoutPanel1.CreateGraphics();单击事件内部时它可以工作,但它会更改主容器中的矩形。

以下是弄乱它的两种方法。有谁知道问题是什么?

    private void DrawIt()
    {
        System.Drawing.Graphics graphics = flowLayoutPanel1.CreateGraphics();
        graphics.Clear(Form1.ActiveForm.BackColor);

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < column; j++)
            {
                System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(50 * j, 50*i, 50, 50);
                rectangleGrid[i, j] = rectangle;
                graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
                graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
            }
        }
    }

    private void flowLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
    {
        int rowIndex = e.Y / 50;
        int columnIndex = e.X / 50;

        System.Drawing.Graphics graphics = flowLayoutPanel2.CreateGraphics();
        System.Drawing.Rectangle rectangle = rectangleGrid[rowIndex, columnIndex];

        graphics.DrawEllipse(System.Drawing.Pens.BlueViolet, rectangle);
        graphics.DrawRectangle(System.Drawing.Pens.Chartreuse, rectangle);
    }
4

0 回答 0