c#:创建CheckBoxes的10x10字段(和数组)

时间:2013-10-22 11:40:51

标签: c# arrays loops checkbox multidimensional-array

我有一个问题,我不知道为什么,但我无法弄清楚它究竟是什么。

我想做的就是创建一个10 x 10的复选框字段,作为游戏的基本板。

我想出的东西应该使用这些方框的数组,根据字段中的空间坐标,从方框[0,0]到方框[9,9]轻松识别它们。

这是我正在努力的代码:

        private void Form1_Load(object sender, EventArgs e)
    {
        // == OPTIONS ========================================
        int xpos = 60;  // Position of the first Checkbox (x)
        int ypos = 60;  // Position of the first Checkbox (y)

        int size = 10;  // Number of Rows and Columns

        int spc = 30;   // Space between boxes (Default:20)
        // ====================================================

        //other Variables
        int x, y;


        //Creating the Game Field
        CheckBox[,] box = new CheckBox[size,size];


        for (int i = 0; i < size; i++)
        {

            for (int j = 0; j < size; j++)
            {

                box[i, j] = new CheckBox();

                //For Debugging Purpuses: Showing i and j next to checkBox
                box[i, j].Text = i + "" + j;

                //Set Position
                y = i * spc + xpos;
                x = j * spc + ypos;
                box[i, j].Location = new Point(x,y);

                this.Controls.Add(box[i, j]);
            }
        }

    }

我从中得到的是一列标有[0,0]到[0,9]的复选框。

即使我在x和y或i和j之间切换,也永远不会改变。所以i.E.我永远不会得到一排复选框,只是一列。我在哪里错了?

我刚刚获得这10个复选框,仅此而已。它们似乎也没有相互叠加。

希望你能在这里帮助我:)谢谢。 蒂莫。

1 个答案:

答案 0 :(得分:1)

默认情况下,复选框太宽。

尝试使用(例如):

int spc = 50;

并在循环中添加:

box[i, j].Width = 40;