c#:创建复选框的 10x10 字段(和数组)

标签 c# arrays loops checkbox multidimensional-array

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

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

我想出的方法应该使用这些框的数组,以便根据它们在字段中的坐标轻松地将它们从 box[0,0] 识别到 box[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 之间切换,也不会改变。所以即我永远不会得到一行复选框,总是只有一列。我哪里出错了?

我只得到那 10 个复选框,仅此而已。它们似乎也没有相互重叠。

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

最佳答案

默认情况下复选框太宽。

试试(例如):

int spc = 50;

同时在循环中添加:

box[i, j].Width = 40;

关于c#:创建复选框的 10x10 字段(和数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19516840/

相关文章:

C#:我可以删除 "{ get; set; }"吗?

c# - 何时在托管模式和非托管模式下使用 C++

javascript - 当用户点击不再显示警报时退出 JavaScript 中的 while 循环

matlab - 如何矢量化这个 for loop matlab

c# - 中间件中的路由 Controller 和 Action

c# - 自定义LiveChart中单个数据的颜色

c - 我的二维数组中数字的频率无法正确输出

C++ , 字符串数组

c - C 中的字符串数组操作

python - R 或 Python - 循环测试数据 - 接下来 24 小时的预测验证(每天 96 个值)